C# List<T>

Summary: in this tutorial, you’ll learn how to use the C# List<T> class to manipulate list elements effectively.

Introduction to the C# List<T> class

The C# List<T> class allows you to manage a list of values of any type. The List<T> class represents a strongly typed list of objects that can be accessed by index.

The T inside the angle brackets specifies the type of elements in the list. For example, you can have a list of integers, strings, and objects.

The List<T> belongs to the System.Collections.Generic namespace.

Creating a list

The following creates a new list of strings:

List<string> states = new List<string>();Code language: C# (cs)

And you can make it shorter by using the var keyword:

var states = new List<string>();Code language: C# (cs)

Adding an element to a list

To add an element to a list, you use the Add() method. For example, the following uses the Add() method to add three strings to the list:

var states = new List<string>();

states.Add("California");
states.Add("Texas");
states.Add("New York");Code language: C# (cs)

Iterating over the elements of a list

To iterate over the elements of a list, you use the foreach statement. The foreach statement assigns the current list element to a variable in each iteration from the first to the last element.

For example, the following uses the foreach statement to iterate over the elements of the states list:

var states = new List<string>();

states.Add("California");
states.Add("Texas");
states.Add("New York");

foreach (var state in states)
{
    Console.WriteLine(state);
}Code language: C# (cs)

Output:

California
Texas
New YorkCode language: C# (cs)

Using collection initializer

To simplify creating and populating a list, you can use a collection initializer. For example, the following creates a list and populates it with three elements:

var states = new List<string>() 
{
    "California",
    "Texas",
    "New York"
};Code language: C# (cs)

Checking if an element exists

To check if an element exists in a list, you use Contains() method. The Contains() method returns true if an element exists in a list. Otherwise, it returns false.

For example:

var states = new List<string>()
{
    "California",
    "Texas",
    "New York"
};

Console.WriteLine(states.Contains("Texas")); // True
Console.WriteLine(states.Contains("Washington")); // FalseCode language: C# (cs)

In this example, the states list contains "Texas". Therefore, the following method call returns true:

states.Contains("Texas");Code language: C# (cs)

Also, because the states list doesn’t contain "Washington", the Contains() method returns false:

states.Contains("Washington");Code language: C# (cs)

Accessing a list element using an index

The List<T> class allows you to access its elements using indices. The first element has an index of 0, the second element has an index of 1, and so on:

listObject[index]Code language: C# (cs)

The following uses the for statement to iterate over the list elements and convert them to uppercase:

var states = new List<string>()
{
    "California",
    "Texas",
    "New York"
};

for (int i = 0; i < states.Count; i++)
{
    states[i] = states[i].ToUpper();
    Console.WriteLine(states[i]);
}Code language: C# (cs)

Output:

CALIFORNIA
TEXAS
NEW YORKCode language: C# (cs)

Inserting an element into a list

To insert an element into a list at a specified index, you use the Insert() method. For example, the following inserts an element at the index 0 and another element at the index 1:

var states = new List<string>()
{
    "California",
    "Texas",
    "New York"
};

states.Insert(0, "Alaska");
states.Insert(1, "Arizona");

foreach (var state in states)
{
    Console.WriteLine(state);
}Code language: C# (cs)

Output:

Alaska
Arizona
California
Texas
New YorkCode language: C# (cs)

Sort a list in ascending or descending order

To sort a list, you use the Sort() method. The Sort() method sorts the list elements using the default comparer.

For example, the following uses the Sort() method to sort a list of strings in ascending order:

var states = new List<string>()
{
    "California",
    "Texas",
    "Arizona",
    "New York",
    "Alaska"
};

states.Sort();


foreach (var state in states)
{
    Console.WriteLine(state);
}Code language: C# (cs)

Output:

Alaska
Arizona
California
New York
TexasCode language: C# (cs)

If you want to sort the list of strings in descending order, you can pass a comparer to the Sort() method like this:

var states = new List<string>()
{
    "California",
    "Texas",
    "Arizona",
    "New York",
    "Alaska"
};

states.Sort((x, y) => y.CompareTo(x));

foreach (var state in states)
{
    Console.WriteLine(state);
}Code language: C# (cs)

Output:

Texas
New York
California
Arizona
AlaskaCode language: C# (cs)

Remove an element from a list

To remove an element from a list, you use the Remove() method. The Remove() method deletes the specified item from a list.

If the item appears multiple times in a list, the Remove() method removes the first occurrence of the item from the list.

The following example uses the Remove() method to remove “Texas” from the list:

var states = new List<string>()
{
    "California",
    "Texas",
    "Arizona",
    "New York",
    "Alaska"
};

states.Remove("Texas");

foreach (var state in states)
{
    Console.WriteLine(state);
}Code language: C# (cs)

Output:

California
Arizona
New York
AlaskaCode language: C# (cs)

Summary

  • Use the List<T> to manage a list of objects.
  • Use the Add() method to add an element to a list
  • Use the Insert() method to insert an element into a list at a specified index
  • Use the Remove() method to remove the first occurrence of an item from a list.
  • Use the Sort() method to sort elements of a list.
  • Use the foreach or for statement to iterate over elements of a list.
  • Use the Contains() method to check if a list contains an element.
Was this tutorial helpful ?