ZetCode

C# List tutorial

last modified July 5, 2020

C# List tutorial shows how to work with a List collection in C#. C# tutorial is a comprehensive tutorial on C# language.

C# List

C# List represents a strongly typed list of objects that can be accessed by index. It provides methods to search, sort, and manipulate lists.

C# List initializer

C# lists can be initialized with literal notation. The elements are added on the right side of the assignment inside {} brackets.

Program.cs
using System;
using System.Collections.Generic;

namespace CollectionInitializer
{
    class Program
    {
        static void Main(string[] args)
        {
            var words = new List<string>{"forest", "oak", "river", "falcon"};

            Console.WriteLine(string.Join(", ", words));
        }
    }
}

In the example, we create a list of strings with list initializer.

var words = new List<string>{"forest", "oak", "river", "falcon"};

A new list is created. Between the angle brackets <>, we specify the data type list.

Console.WriteLine(string.Join(", ", words));

To get a quick look at the contents of the list, we join all the values into a string, separated by comma.

$ dotnet run
forest, oak, river, falcon

This is the output.

C# List count elements

With the Count property, we get the number of elements in the list.

Program.cs
using System;
using System.Collections.Generic;

namespace Counting
{
    class Program
    {
        static void Main(string[] args)
        {
            var vals = new List<int>{ 0, 1, 2, 3, 4, 5 };

            Console.WriteLine($"There are {vals.Count} elements in the list");
        }
    }
}

The example counts the number of elements in the list.

Console.WriteLine($"There are {vals.Count} elements in the list");

Here we print the number of elements in the List.

$ dotnet run
There are 6 elements in the list

This is the output.

C# List access elements

Elements of a list can be accessed using the index notation []. The index is zero-based.

Program.cs
using System;
using System.Collections.Generic;

namespace AccessElements
{
    class Program
    {
        static void Main(string[] args)
        {
            var vals = new List<int>{ 0, 1, 2, 3, 4, 5 };

            Console.WriteLine(vals[0]);
            Console.WriteLine(vals[1]);

            Console.WriteLine(vals.Count - 1);
        }
    }
}

The example prints the first, second, and the last element of the list.

Console.WriteLine(vals.Count - 1);

To get the last element of the list, we count the number of elements and divide one.

$ dotnet run
0
1
5

This is the output.

C# List add elements

The Add method adds an element at the end of the list. The AddRange methods adds the elements of the specified collection to the end of the list.

Program.cs
using System;
using System.Collections.Generic;

namespace AddElements
{
    class Program
    {
        static void Main(string[] args)
        {
            var words = new List<string> { "forest", "oak", "river", "falcon" };

            words.Add("sky");
            words.Add("ocean");

            var words2 = new string[] {"owl", "hawk"};
            words.AddRange(words2);

            Console.WriteLine(string.Join(',', words));

        }
    }
}   

The example creates a new list and adds new elements to it.

words.Add("sky");
words.Add("ocean");

Two elements are added to the list with Add.

var words2 = new string[] {"owl", "hawk"};
words.AddRange(words2);

With AddRange method, we add another collection to the list.

$ dotnet run
forest,oak,river,falcon,sky,ocean,owl,hawk

This is the output.

C# List insert elements

The Insert method inserts an element into the list at the specified index. The InsertRange inserts the elements of a collection into the list at the specified index.

Program.cs
using System;
using System.Collections.Generic;

namespace InsertElements
{
    class Program
    {
        static void Main(string[] args)
        {
            var words = new List<string> { "forest", "oak", "river", "falcon" };

            words.Insert(0, "sky");
            words.Insert(words.Count, "cloud");

            var words2 = new List<string> {"water", "wave"};
            words.InsertRange(2, words2);

            Console.WriteLine(string.Join(", ", words));
        }
    }
}  

The example creates a new list of words and inserts new elements to it.

$ dotnet run
sky, forest, water, wave, oak, river, falcon, cloud

This is the output.

C# List Contains

The Contains method determines whether the element is in the list.

Program.cs
using System;
using System.Collections.Generic;

namespace ListContains
{
    class Program
    {
        static void Main(string[] args)
        {
            var words = new List<string> { "forest", "oak", "river", "falcon" };

            if (words.Contains("oak")) 
            {

                Console.WriteLine("The list contains the oak word");
            }
        }
    }
}

In the example, we check if the oak word is in the list.

C# List removing elements

The Remove, RemoveAt, RemoveAll, RemoveRange, and Clear methods can be used to remove elements from a list.

Program.cs
using System;
using System.Collections.Generic;

namespace Removing
{
    class Program
    {
        static void Main(string[] args)
        {
            var nums = new List<int> {0, 1, 2, -3, 4, -5, 6, 7, -8, 9, 10};

            nums.RemoveAll(e => e < 0);
            Console.WriteLine(string.Join(", ", nums));

            nums.Remove(0);
            nums.RemoveAt(nums.Count - 1);

            Console.WriteLine(string.Join(", ", nums));

            nums.RemoveRange(0, 3);

            Console.WriteLine(string.Join(", ", nums));

            nums.Clear();
            Console.WriteLine("{0}", nums.Count);
        }
    }
}

In the example, we remove elements from the list of integers.

nums.RemoveAll(e => e < 0);
Console.WriteLine(string.Join(", ", nums));

With RemoveAll method, we remove all elements that satisfy the given predicate; in our case, we remove all negative values.

nums.Remove(0);

The Remove method removes the first occurrence of the 0 value.

nums.RemoveAt(nums.Count - 1);

With RemoveAt, we remove the element at the given index.

nums.RemoveRange(0, 3);

The RemoveRange method removes the given range of values from the list. The parameters are the zero-based starting index of the range of elements to remove and the number of elements to remove.

C# List ToArray

The ToArray method copies the elements of a list into an array.

Program.cs
using System;
using System.Collections.Generic;

namespace ToArray
{
    class Program
    {
        static void Main(string[] args)
        {
            var nums = new List<int> {1, 2, 3, 4};

            Console.WriteLine(nums.GetType());

            var nums2 = nums.ToArray();
            Console.WriteLine(nums2.GetType());
        }
    }
}

In the example, we create an array from a list. We print the type of both containers with GetType.

$ dotnet run
System.Collections.Generic.List`1[System.Int32]
System.Int32[]

This is the output.

C# List ForEach

The ForEach method performs the specified action on each element of a list.

Program.cs
using System;
using System.Collections.Generic;

namespace ListForEach
{
    class Program
    {
        static void Main(string[] args)
        {
            var words = new List<string> { "forest", "oak", "river", "falcon" };

            words.ForEach(Console.WriteLine);
            words.ForEach(word => Console.WriteLine(word.ToUpper()));
        }
    }
}

We call the ForEach method twice on a list of words.

words.ForEach(Console.WriteLine);

We print all elements of the list.

words.ForEach(word => Console.WriteLine(word.ToUpper()));

Here we also print all elements of the list; the words are transformed to uppercase.

$ dotnet run
forest
oak
river
falcon
FOREST
OAK
RIVER
FALCON

This is the output.

C# loop List

There are several ways to loop over a C# list.

Program.cs
using System;
using System.Collections.Generic;

namespace Traversing
{
    class Program
    {
        static void Main(string[] args)
        {
            var nums = new List<int> {1, 2, 3, 4};

            nums.ForEach(e => Console.WriteLine(e));

            Console.WriteLine("********************");

            foreach(int e in nums) 
            {
                
                Console.WriteLine(e);
            }

            Console.WriteLine("********************");

            for (int i = 0; i < nums.Count; i++) 
            {
                
                Console.WriteLine(nums[i]);
            }
        }
    }
}

The example shows three ways of looping over a list in C#.

nums.ForEach(e => Console.WriteLine(e));

We loop over a list with ForEach.

foreach(int e in nums) 
{
    
    Console.WriteLine(e);
}

In this case, we use the foreach statement.

for (int i = 0; i < nums.Count; i++) 
{
    
    Console.WriteLine(nums[i]);
}

Finally, this is the classic for loop.

$ dotnet run
1
2
3
4
********************
1
2
3
4
********************
1
2
3
4

This is the output.

C# sort, reverse List

The Sort method sorts the elements of a list, the Reverse method reverses the elements of the list.

Program.cs
using System;
using System.Collections.Generic;

namespace SortReverse
{
    class Program
    {
        static void Main(string[] args)
        {
            var nums = new List<int> {2, 1, 5, 4, 3};

            nums.Reverse();
            Console.WriteLine(string.Join(',', nums));

            nums.Sort();
            Console.WriteLine(string.Join(',', nums));
        }
    }
}

The example reverses the elements of the list of integers and then sorts them.

$ dotnet run
3,4,5,1,2
1,2,3,4,5

This is the output.

C# List FindAll

The FindAll method retrieves all the elements of a list that match the conditions defined by the specified predicate. It returns a list containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty list.

A predicate is a method that returns a boolean value. To learn more about predicates, visit the C# Predicate tutorial.

Program.cs
using System;
using System.Collections.Generic;

namespace ListFindAll
{
    class Program
    {
        static void Main(string[] args)
        {
            var vals = new List<int> {-1, -3, 0, 1, 3, 2, 9, -4};

            List<int> filtered = vals.FindAll(e => e > 0);

            Console.WriteLine(string.Join(',', filtered));
        }
    }
}

The example finds all positive elements.

List<int> filtered = vals.FindAll(e => e > 0);

The predicate is an expression that returns true for values greater than zero.

$ dotnet run
1,3,2,9

These are the positive values of the list.

C# List Find, FindLast, FindIndex, FindLastIndex

The Find method returns the first occurrence of the element that matches the given predicate. The FindLast method returns the last occurrence of the element that matches the given predicate.

The FindIndex method returns the index of the first occurrence of the element that matches the given predicate. The FindLastIndex method returns the index of the last occurrence of the element that matches the given predicate.

Program.cs
using System;
using System.Collections.Generic;

namespace Finding
{
    class Program
    {
        static void Main(string[] args)
        {
            var nums = new List<int> { 6, -2, 1, 5, 4, 3, 2, 9, -1, 7 };

            var found = nums.Find(e => e < 0);
            Console.WriteLine(found);

            var found2 = nums.FindIndex(e => e < 0);
            Console.WriteLine(found2);

            var found3 = nums.FindLast(e => e < 0);
            Console.WriteLine(found3);

            var found4 = nums.FindLastIndex(e => e < 0);
            Console.WriteLine(found4);
        }
    }
}

The example uses all the mentioned methods to find elements and indexes.

$ dotnet run
-2
1
-1
8

This is the output.

C# List ConvertAll

The ConvertAll method converts the elements in the current List to another type, and returns a list containing the converted elements.

Program.cs
using System;
using System.Collections.Generic;

namespace ConvertAll
{
    class Program
    {
        static void Main(string[] args)
        {
            var words = new List<string>();

            words.Add("falcon");
            words.Add("owl");
            words.Add("sky");
            words.Add("hawk");
            words.Add("stork");

            List<string> uppedWords = words.ConvertAll(s => s.ToUpper());
            List<int> lengths = words.ConvertAll(s => s.Length);

            Console.WriteLine(string.Join(", ", uppedWords));
            Console.WriteLine(string.Join(", ", lengths));
        }
    }
}

In the example, we have a list of words. We convert the list to two other lists.

List<string> uppedWords = words.ConvertAll(s => s.ToUpper());

Here we convert the list to a list containing words transformed into uppercase.

List<int> lengths = words.ConvertAll(s => s.Length);

In the second case, the converted list contains integers that are the length of the words in the original list.

$ dotnet run
FALCON, OWL, SKY, HAWK, STORK
6, 3, 3, 4, 5

This is the output.

In the second example, we have a separate squareRoot method, which is applied on the list of integers.

Program.cs
using System;
using System.Collections.Generic;

namespace ConvertAll2
{
    class Program
    {
        static double squareRoot(int x)
        {
            return Math.Sqrt(x);
        }

        static void Main(string[] args)
        {
            var vals = new List<int> { 1, 4, 9, 16, 25 };

            Converter<int, double> converter = squareRoot;
            List<double> vals2 = vals.ConvertAll<double>(converter);

            System.Console.WriteLine(string.Join(", ", vals2));
        }
    }
}

In the example, we create a new list by applying square root operation on the list of integers.

$ dotnet run
1, 2, 3, 4, 5

This is the output.

C# List TrueForAll

The TrueForAll determines whether every element in the list matches the conditions defined by the given predicate.

Program.cs
using System;
using System.Collections.Generic;

namespace TrueForAll
{
    class Program
    {
        static void Main(string[] args)
        {
             var nums = new List<int> {1, 2, 3, 4, 5, 6, 7, 8};

             var res1 = nums.TrueForAll(e => e % 2 == 0);
             Console.WriteLine(res1);
             
             var res2 = nums.TrueForAll(e => e > 0);
             Console.WriteLine(res2);
        }
    }
}

In the example, we check if the elements in the list are all even and that they are all positive.

$ dotnet run
False
True

Not all elements are even and all elements are positive.

In this tutorial, we have worked with a C# List collection.

List all C# tutorials.