C# Exception Handling
C# File I/O
C# Delegates and Events
C# Generics
C# Async Programming
C# List

C# List

List<T> is the go-to dynamic collection — a resizable array of typed elements.

1 - Creating and Adding

var numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Add(6);
numbers.AddRange(new[] { 7, 8, 9 });
numbers.Insert(0, 0); // insert at index

Console.WriteLine(numbers.Count); // 10

2 - Accessing and Searching

Console.WriteLine(numbers[0]);          // 0
Console.WriteLine(numbers[^1]);         // 9 (last)

bool has5 = numbers.Contains(5);       // true
int idx   = numbers.IndexOf(5);        // 5
int found = numbers.Find(x => x > 7);  // 8
var big   = numbers.FindAll(x => x > 5); // [6,7,8,9]

3 - Removing

numbers.Remove(0);           // remove first matching value
numbers.RemoveAt(0);         // remove by index
numbers.RemoveAll(x => x % 2 == 0); // remove all even

4 - Sorting and Iterating

var names = new List<string> { "Charlie", "Alice", "Bob" };
names.Sort();                        // alphabetical
names.Sort((a, b) => b.CompareTo(a)); // reverse

foreach (string name in names)
    Console.WriteLine(name);

// LINQ on List
var filtered = names.Where(n => n.StartsWith("A")).ToList();

Note: Use List<T> when you need a resizable ordered collection with index access. If the size is fixed, prefer T[] — it is faster and uses less memory.

-Tip-