C# List Usage: Complete Guide with Examples and Methods
A List in C# is a dynamic collection that allows you to store and manage a group of elements of the same type. Unlike arrays, lists can grow and shrink in size automatically, making them much more flexible for real-world applications.
The List class is part of the System.Collections.Generic namespace and is one of the most commonly used data structures in C#.
1. What is a List in C#?
A List is a generic collection that stores elements in a specific order and allows duplicate values. It works similarly to arrays but provides additional functionality such as dynamic resizing, easy insertion, removal, and searching.
Basic Example:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
Console.WriteLine(numbers[0]); // Output: 10
}
}
2. Why Use List Instead of Array?
Advantages of List
• Dynamic Size: Automatically resizes when elements are added or removed.
• Easy to Use: Built-in methods simplify operations like adding, removing, and searching.
• Flexible: No need to define size in advance.
• Rich API: Comes with many useful methods.
Disadvantages of List
• Slightly more memory usage than arrays.
• Slightly slower in some performance-critical scenarios.
3. Creating and Initializing a List
Syntax:
List<Type> listName = new List<Type>();
Examples:
List<string> names = new List<string>();
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
4. Adding Elements to a List
Add() Method
numbers.Add(10);
AddRange() Method
numbers.AddRange(new int[] { 20, 30, 40 });
5. Accessing List Elements
Lists use indexing, just like arrays.
Console.WriteLine(numbers[0]); // First element
6. Removing Elements from a List
numbers.Remove(10); // Removes first occurrence
numbers.RemoveAt(0); // Removes by index
numbers.Clear(); // Removes all elements
7. Iterating Through a List
Using for Loop:
for (int i = 0; i < numbers.Count; i++)
{
Console.WriteLine(numbers[i]);
}
Using foreach:
foreach (int num in numbers)
{
Console.WriteLine(num);
}
8. Common List Methods
| Method | Description |
|---|---|
| Add() | Adds an element to the list |
| Remove() | Removes the first occurrence of a specific element |
| RemoveAt() | Removes the element at the specified index |
| Clear() | Removes all elements from the list |
| Contains() | Checks if an element exists in the list |
| IndexOf() | Returns the index of the first occurrence of an element |
| Sort() | Sorts the elements in the list |
| Reverse() | Reverses the order of elements in the list |
| Count | Gets the total number of elements in the list |
Example:
if (numbers.Contains(10))
{
Console.WriteLine("Number exists!");
}
9. Example: Complete List Usage
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Cherry");
Console.WriteLine("Fruits List:");
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
fruits.Remove("Banana");
Console.WriteLine("After removal:");
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
}
}
Output:
Fruits List:
Apple
Banana
Cherry
After removal:
Apple
Cherry
10. When Should You Use List?
Use a List when
• You don’t know the size of data in advance
• You need to frequently add/remove items
• You want built-in helper methods
Use an Array when
• Size is fixed
• Performance is critical
11. Common Mistakes
• Accessing invalid index
numbers[10]; // Error if index doesn't exist
• Forgetting using System.Collections.Generic;
• Confusing Count with Length
- List uses Count
- Array uses Length
12. Best Practices
• Use List instead of arrays for dynamic data
• Use foreach when you don’t need index
• Always check bounds before accessing elements
• Use built-in methods instead of manual loops when possible