csharp-que-son-listas

What are Lists and how to use them in C#

  • 6 min

A List is a generic collection that implements an array with dynamic size. It is one of the most frequently used data structures.

Unlike arrays, lists allow dynamic manipulation of their elements (meaning we can add and remove elements, define a fixed size at the start).

The List<T> class in the System.Collections.Generic namespace allows creating lists of any type specified by the type parameter T.

If you want to learn more, check out the Introduction to Programming Course

Declaring lists

To declare a list in C#, the following syntax is used:

List<type> listName;
Copied!

For example, to declare a list of integers:

List<int> numbers;
Copied!

Creating the list

Once the list is declared, before we can use it we must initialize it. To do this we need to create a new List and assign it to the variable we declared earlier.

Or, alternatively, we can do it at the same time we declare our List. Like this:

List<int> numbers = new List<int>();

// equivalent
var numbers = new List<int>();
List<int> numbers = new ();
Copied!

Initializing lists

Alternatively, if we want to initialize the list to a series of known values at compile time, we can do it using the semantic initializer [].

// recommended syntax
List<int> numbers = [ 1, 2, 3, 4, 5 ];

// equivalent to this
List<int> numbers = new () { 1, 2, 3, 4, 5 };
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };
Copied!

Basic list usage

Accessing list elements

List elements can be accessed using indices, starting from 0:

List<int> numbers = new List<int>();

int firstNumber = numbers[0]; // firstNumber will be 1
Copied!

Modifying list elements

List elements can be modified by assigning new values to specific indices:

numbers[1] = 20; // The second element of the list will now be 20
Copied!

Adding elements to a list

To add elements to a list, the Add method is used:

numbers.Add(1);
Copied!

Furthermore, we can add multiple elements at once using the AddRange() method:

list.AddRange(collection);
Copied!

Where collection represents a collection of elements of the same type as the list.

Removing elements from a list

To remove an element from a list, we can use the Remove() method:

numbers.Remove(20); // Removes the first element with the value 20
Copied!

It is also possible to remove an element based on its index using the RemoveAt() method:

numbers.RemoveAt(0); // Removes the first element
Copied!

Clearing the entire list

numbers.Clear(); // Removes all elements from the list
Copied!

Inserting elements at a specific position

To insert an element at a specific position in the list, the Insert method is used:

numbers.Insert(1, 15); // Inserts the number 15 at position 1
Copied!

Useful properties and methods of List

Lists in C# offer a wide range of operations that allow us to manipulate and access their elements efficiently. Let’s look at some of them:

Count property

The Count property returns the number of elements in the list:

int count = numbers.Count; // count will be the number of elements in the list
Copied!

Contains method

The Contains method checks if a specific value is present in the list:

bool contains = numbers.Contains(3); // contains will be true if the number 3 is in the list
Copied!

IndexOf method

The IndexOf method returns the index of the first occurrence of a specific value in the list:

int index = numbers.IndexOf(3); // index will be the position of the number 3 in the list
Copied!

Sort method

The Sort method sorts the elements of the list:

numbers.Sort(); // Sorts the elements in ascending order
Copied!

Reverse method

The Reverse method reverses the order of the elements in the list:

numbers.Reverse(); // Reverses the order of the elements in the list
Copied!

Practical examples