Quick Answer
Essential operations with generic lists.
Understanding the Issue
List<T> is a dynamically resizable array. Provides methods for adding, removing, finding, and sorting elements with type safety.
The Problem
This code demonstrates the issue:
Csharp
Error
// Need to create and manipulate a list
The Solution
Here's the corrected code:
Csharp
Fixed
List<string> fruits = new() { "Apple", "Banana" };
// Adding
fruits.Add("Orange");
fruits.Insert(0, "Mango");
// Removing
fruits.Remove("Apple");
fruits.RemoveAt(0);
// Searching
bool hasBanana = fruits.Contains("Banana");
int index = fruits.FindIndex(f => f.StartsWith("B"));
// Sorting
fruits.Sort(); // Alphabetical
fruits.Sort((a,b) => b.Length.CompareTo(a.Length)); // Custom
Key Takeaways
List<T> provides flexible collection operations with type safety.