List Operations and Methods
Working with lists in C# becomes much more powerful when you use built-in methods to search, sort, and modify your data. Some of the most commonly used methods include:
Contains: checks if a list has a specific value;IndexOf: finds the index of the first occurrence of a value in the list;Sort: arranges the elements of the list in ascending order (alphabetically for strings, numerically for numbers);Reverse: reverses the order of the elements in the list.
These methods help you quickly find, organize, and update your data without writing complex logic from scratch.
Program.cs
123456789101112131415161718192021222324252627using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> fruits = new List<string> { "Banana", "Apple", "Cherry", "Date" }; // Search for an item in the list string searchItem = "Apple"; bool hasApple = fruits.Contains(searchItem); Console.WriteLine($"List contains '{searchItem}': {hasApple}"); // Sort the list alphabetically fruits.Sort(); Console.WriteLine("Sorted list:"); foreach (string fruit in fruits) { Console.WriteLine(fruit); } } } }
In this example, you start with a list of fruit names. The Contains method checks if "Apple" is present in the list and prints the result. Then, the Sort method arranges the list in alphabetical order. After sorting, you print each fruit to show the new order. Searching and sorting with these methods is efficient and requires only a single line of code for each operation.
Program.cs
1234567891011121314151617181920212223242526using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> colors = new List<string> { "Red", "Green", "Blue", "Yellow" }; // Reverse the list colors.Reverse(); Console.WriteLine("Reversed list:"); foreach (string color in colors) { Console.WriteLine(color); } // Find the index of "Green" int greenIndex = colors.IndexOf("Green"); Console.WriteLine($"Index of 'Green': {greenIndex}"); } } }
1. Which method checks if a List contains a specific value?
2. What does the Sort() method do to a List?
3. Fill in the blanks to reverse a List<string> named colors.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you show me an example of how to use the Reverse method with a list?
What happens if I search for an item that isn't in the list?
Can these methods be used with lists of numbers as well as strings?
Awesome!
Completion rate improved to 4.76
List Operations and Methods
Swipe to show menu
Working with lists in C# becomes much more powerful when you use built-in methods to search, sort, and modify your data. Some of the most commonly used methods include:
Contains: checks if a list has a specific value;IndexOf: finds the index of the first occurrence of a value in the list;Sort: arranges the elements of the list in ascending order (alphabetically for strings, numerically for numbers);Reverse: reverses the order of the elements in the list.
These methods help you quickly find, organize, and update your data without writing complex logic from scratch.
Program.cs
123456789101112131415161718192021222324252627using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> fruits = new List<string> { "Banana", "Apple", "Cherry", "Date" }; // Search for an item in the list string searchItem = "Apple"; bool hasApple = fruits.Contains(searchItem); Console.WriteLine($"List contains '{searchItem}': {hasApple}"); // Sort the list alphabetically fruits.Sort(); Console.WriteLine("Sorted list:"); foreach (string fruit in fruits) { Console.WriteLine(fruit); } } } }
In this example, you start with a list of fruit names. The Contains method checks if "Apple" is present in the list and prints the result. Then, the Sort method arranges the list in alphabetical order. After sorting, you print each fruit to show the new order. Searching and sorting with these methods is efficient and requires only a single line of code for each operation.
Program.cs
1234567891011121314151617181920212223242526using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> colors = new List<string> { "Red", "Green", "Blue", "Yellow" }; // Reverse the list colors.Reverse(); Console.WriteLine("Reversed list:"); foreach (string color in colors) { Console.WriteLine(color); } // Find the index of "Green" int greenIndex = colors.IndexOf("Green"); Console.WriteLine($"Index of 'Green': {greenIndex}"); } } }
1. Which method checks if a List contains a specific value?
2. What does the Sort() method do to a List?
3. Fill in the blanks to reverse a List<string> named colors.
Thanks for your feedback!