By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
List is a generic collection class in C# that allows you to create a list of objects of a specific type. It is part of the System.Collections.Generic namespace. Mastering List is crucial for any C# developer because it provides dynamic array functionality, which is essential for managing collections of data. Understanding how to Add, Remove, Find, and Sort elements in a List is fundamental for efficient data manipulation. Incorrect usage can lead to runtime errors, inefficient code, and hard-to-debug issues. For example, improperly removing elements can cause IndexOutOfRangeException, affecting application stability.
List<int> numbers = new List<int>();
⚠️ Pitfall: Forgetting to specify the type will result in a compilation error.
Add Elements
numbers.Add(5); numbers.Add(10);
⚠️ Pitfall: Adding elements of the wrong type will cause a compilation error.
Remove Elements
numbers.Remove(5);
⚠️ Pitfall: Removing an element that does not exist will not throw an error but will return false.
Find Elements
int found = numbers.Find(n => n > 5);
⚠️ Pitfall: Using an incorrect predicate can result in no matches found.
Sort Elements
numbers.Sort();
Experts view List as a versatile tool for dynamic data management. They understand the underlying data structures and algorithms, allowing them to optimize performance and avoid common pitfalls. Instead of memorizing methods, they think in terms of collection manipulation patterns and efficiency.
Exam trap: Questions that omit the type parameter.
The mistake: Adding elements of the wrong type.
Exam trap: Mixed-type elements in code snippets.
The mistake: Removing non-existent elements.
Exam trap: Questions that require element removal without verification.
The mistake: Incorrect predicate in Find.
Exam trap: Complex predicates in code snippets.
The mistake: Sorting lists with incomparable types.
Scenario: You have a list of student names and need to add, remove, find, and sort them.Question: Perform the following operations: add "Alice" and "Bob", remove "Alice", find a name starting with "B", and sort the list.Solution: 1. Initialize the list: List<string> students = new List<string>(); 2. Add elements: students.Add("Alice"); students.Add("Bob"); 3. Remove element: students.Remove("Alice"); 4. Find element: string found = students.Find(name => name.StartsWith("B")); 5. Sort the list: students.Sort(); Answer: The final list is ["Bob"].Why it works: Each operation correctly manipulates the list according to the specified criteria.
List<string> students = new List<string>();
students.Add("Alice"); students.Add("Bob");
students.Remove("Alice");
string found = students.Find(name => name.StartsWith("B"));
students.Sort();
["Bob"]
Scenario: You have a list of integers and need to find the first even number.Question: Find the first even number in the list [3, 7, 2, 5, 8].Solution: 1. Initialize the list: List<int> numbers = new List<int> { 3, 7, 2, 5, 8 }; 2. Find the first even number: int evenNumber = numbers.Find(n => n % 2 == 0); Answer: The first even number is 2.Why it works: The predicate correctly identifies even numbers.
[3, 7, 2, 5, 8]
List<int> numbers = new List<int> { 3, 7, 2, 5, 8 };
int evenNumber = numbers.Find(n => n % 2 == 0);
2
Scenario: You have a list of temperatures and need to sort them in descending order.Question: Sort the list [22, 15, 30, 10] in descending order.Solution: 1. Initialize the list: List<int> temperatures = new List<int> { 22, 15, 30, 10 }; 2. Sort the list: temperatures.Sort(); temperatures.Reverse(); Answer: The sorted list is [30, 22, 15, 10].Why it works: Sorting followed by reversing achieves descending order.
[22, 15, 30, 10]
List<int> temperatures = new List<int> { 22, 15, 30, 10 };
temperatures.Sort(); temperatures.Reverse();
[30, 22, 15, 10]
Add(T item)
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.