C# List Tutorial For Beginners With Examples

C# List is one of the most popular collection class. A List object can dynamically increase or decrease in size.
Another collection class Arraylist is also similar to a List class. The primary difference between an Arraylist and a List is that a List is strongly typed class whereas an Arraylist is not.

What is a Strongly Typed Collection Class?

When the type of objects that can be stored in a collection class can be predefined then those type of Collection Classes are called as strongly typed.
These type of classes for which data type can be predefined are called as Generic types. In C#, the generic List class is available.

Important features of C# List class

Some important features of List class are as below:
  • It implements the IList generic interface. There are also many other collections like Arraylist which implements the IList interface.
  • A List is not sorted by default.
  • It allows duplicate values.
  • It allows null as a valid value.
  • Index can be used to access an item in a List.
  • List objects are thread safe.
  • Performance wise List is better than Arraylist.

Create a C# List object

A List object can be created as below:
    List<int> listOfNumbers = new List<int>();
    
The List class is present in the System.Collections.Generic namespace.
In the above example the List object listOfNumbers can store any object that is of type int.
If we try to add an object of type string to the List listOfNumbers then the compiler will complain. Since we are able to define the type of data that can be stored in the List object hence it is strongly typed.

Add items to a List in C#

Objects can be added to a List using the Add method.
As shown in the below example three numbers are added to the List listOfNumbers:
    listOfNumbers.Add(10);
    listOfNumbers.Add(20);
    listOfNumbers.Add(30);
    
Using the Count property the number of items present in a List can be determined.
    Console.WriteLine(listOfNumbers.Count);
    
Output: 3

Remove items from a List in C#

To remove an item from a List the Remove method can be used as shown below:
     listOfNumbers.Remove(20);

To check how many items are present in the List listOfNumbers we can use the Count property again.
    Console.WriteLine(listOfNumbers.Count);
    
Output: 2

Access an item in a C# List using Index

Indexes can be used to access items of a List. These indexes are zero based. Indexes gives direct access to an item of a list.
    Console.WriteLine(listOfNumbers[0]);
    
Output: 10

For Loop to access items in a C# List

In order to read multiple items from a List efficiently a for loop can be used.
    List<int> listOfNumbers = new List<int>();

    listOfNumbers.Add(10);
    listOfNumbers.Add(20);
    listOfNumbers.Add(30);

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

Foreach Loop to access items in a C# List

Similar to for loop, foreach loop can be used to read items from a List object without using an index.
    List<int> listOfNumbers = new List<int>();

    listOfNumbers.Add(10);
    listOfNumbers.Add(20);
    listOfNumbers.Add(30);

    foreach(int item in listOfNumbers)
    {
        Console.WriteLine(item);    
    }
            

Sort a C# List

A List in C# is always unsorted. The items are stored in the same sequence in which they are added to the list.
To sort these items .NET framework has provided a sort method. By default the sort method arranges or orders the items of a C# list in ascending order.
    
    List<int> listOfNumbers = new List<int>();

    listOfNumbers.Add(100);
    listOfNumbers.Add(10);
    listOfNumbers.Add(30);

    Console.WriteLine("Before Sorting the List");

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

    listOfNumbers.Sort();

    Console.WriteLine("After Sorting the List");
    for (int index = 0; index < listOfNumbers.Count; index++)
    {
        Console.WriteLine(listOfNumbers[index]);
    }

    Console.ReadLine();
        
Output:
Before Sorting the List 
100
10
30
After Sorting the List 
10
30
100

Find an item in a C# List

There are number of ways to find a specific item in a List. The Find method is one such way. It returns the first matching item from a List.
The Find method accepts a lambda expression as a parameter. Using this lambda expression a condition can be specified. The first item in the List which matches this condition is returned by the Find method.
    List<string> PLanguages = new List<string>();

    PLanguages.Add("Pascal");
    PLanguages.Add("Visual Basic");
    PLanguages.Add("Java");
    PLanguages.Add("C Sharp");
    PLanguages.Add("Python");

    string result = PLanguages.Find(x => x.Length < 5);

    Console.WriteLine("Language name with less than five characters is " + result);

    Console.ReadLine();
            
Output:
Language name with less than five characters is Java
Another way to find a specific item or element in a List is by using BinarySearch method. This method returns the index of the first item which matches the provided string.
To use this method correctly the List should be sorted first. Below example shows how to use the BinarySearch method.
    List<string> PLanguages = new List<string>();

    PLanguages.Add("Pascal");
    PLanguages.Add("Visual Basic");
    PLanguages.Add("Python");
    PLanguages.Add("Java");
    PLanguages.Add("C Sharp");
    PLanguages.Add("Python2");

    PLanguages.Sort();

    int position = PLanguages.BinarySearch("Python");

    Console.WriteLine("Position of element with name Python is " + position);
            
    Console.ReadLine();
        

Methods and Properties of a C# List

Commonly used methods of an List are:
MethodDescription
AddAdds an item at the end of a List.
AddRangeAdds the elements of an collection to the end of a List.
BinarySearchSearches for an item in a sorted List and returns the index of the first item found.
ClearRemoves all the elements from a List.
ContainsChecks if an item is present in a List.
FindReturns the first item matching a condition in a List.
IndexOf(Object)Determines the position of an object and returns the zero-based index of that object in a List.
InsertInserts an item in a List at a specified index.
InsertRangeInserts the elements of an collection at a specific index of a List.
RemoveSearches for an item and removes the first occurrence of that item from a List.
RemoveAtRemoves an item from a List at a specific index.
RemoveRangeRemoves a contiguous list of items from a List.
ReverseReverses the order of the items present in a List.
SortSorts the items of a List.
Commonly used properties of an List are:
PropertyDescription
CapacityThis property tells how much data a List object can hold. This property can also be used to set the capacity of a List object.
CountReturns the number of items present in List.
ItemThis property is used along with an index to get or set a value.