Working with Lists in C#: Complete List<T> Tutorial
The List<T> class is one of the most commonly used collection
types in C#. It provides a flexible way to store and manage groups of
objects while allowing items to be added, removed, searched, sorted,
and modified dynamically.
Whether you're building web applications, desktop software, APIs, games, or data-processing tools, understanding how to work with lists is an essential skill for every C# developer.
What Is List<T>?
A List is a generic collection that stores multiple values of the same type. Unlike arrays, lists can grow and shrink dynamically during program execution.
List<string> names = new List<string>();
The generic type parameter <T> determines the type of
data the list can store.
Creating a List
Before using a list, you must create an instance of the List class.
using System.Collections.Generic;
List<string> fruits = new List<string>();
You can also initialize a list with values.
List<string> fruits = new List<string>
{
"Apple",
"Banana",
"Orange"
};
Adding Items to a List
The Add() method inserts a new item at the end of the list.
List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Orange");
You can add as many items as needed because lists automatically resize when necessary.
Accessing List Items
List items are accessed using their index position.
Console.WriteLine(fruits[0]);
Console.WriteLine(fruits[1]);
Output
Apple
Banana
Remember that indexing starts at zero.
Looping Through a List
The foreach loop is commonly used to process all items in a list.
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
This approach is simple, readable, and suitable for most situations.
Removing Items
Lists provide several methods for removing data.
fruits.Remove("Banana");
Remove() deletes the first matching item.
fruits.RemoveAt(0);
RemoveAt() deletes an item based on its index position.
fruits.Clear();
Clear() removes every item from the list.
Searching Lists
The Contains() method checks whether an item exists in a list.
bool exists = fruits.Contains("Apple");
Console.WriteLine(exists);
Output
True
You can also find an item's index.
int index = fruits.IndexOf("Orange");
Console.WriteLine(index);
Sorting Lists
Sorting helps organize data in ascending or descending order.
fruits.Sort();
To reverse the order:
fruits.Reverse();
Sorting is useful when displaying reports, search results, or user data.
Filtering Lists with LINQ
LINQ makes it easy to filter and transform data.
using System.Linq;
List<int> numbers = new List<int>
{
10, 20, 30, 40, 50
};
var results = numbers.Where(n => n > 25);
foreach (var number in results)
{
Console.WriteLine(number);
}
Output
30
40
50
LINQ is widely used in modern C# applications because it provides powerful querying capabilities with minimal code.
List Performance Tips
While lists are highly flexible, understanding performance can help you write more efficient applications.
- Use List<T> when the collection size may change.
- Use arrays when the size is fixed.
- Avoid unnecessary loops.
- Use LINQ carefully on very large datasets.
- Reserve capacity when you know the expected size.
List<int> numbers = new List<int>(1000);
Setting an initial capacity can reduce memory reallocations.
Common List Methods
| Method | Description |
|---|---|
| Add() | Adds an item |
| Remove() | Removes an item |
| RemoveAt() | Removes by index |
| Contains() | Checks existence |
| Sort() | Sorts items |
| Reverse() | Reverses order |
| Clear() | Removes all items |
| Count | Returns item count |
Frequently Asked Questions
What is the difference between an array and a List?
Arrays have a fixed size, while lists can grow and shrink dynamically during program execution.
When should I use List<T>?
Use lists whenever the number of items may change or when you need built-in methods for searching, sorting, and managing collections.
Are lists slower than arrays?
Lists have a small overhead because of their flexibility, but they are efficient for most business and web applications.
Can a list store custom objects?
Yes. Lists can store classes, structs, and virtually any data type.
Conclusion
The List<T> class is one of the most useful collections available in C#. It provides a simple and efficient way to store, manage, search, sort, and manipulate data.
By mastering lists, you'll be better prepared to build scalable .NET applications, process data efficiently, and write cleaner code.