Hit enter after type your search item

An Introduction to C# List

A few main points about the List in C#

  • The C# List <T> is the part of System.Collection.Generic Namespace that represents a strongly typed list of objects.
  • The List is just like the ArrayList except the later is non-generic.
  • The arrays are fixed size while List <T> can grow as needed automatically.
  • Other generic classes in the System.Collection.Generic Namespace includes Dictionary, Queue, and Stack.
  • A list is created by using the new keyword as shown in the example in the next section.
  • The index of the list starts at 0.
  • You may use Add method of List class for adding new elements at the end of a list.
  • For removing all elements from the list, you may use the Clear method.
  • For adding a new element at the specified position, use the Insert method of the List class.
  • For removing a specified element from the list, use the Remove method.
  • In order to sort a list, the List class has the Sort method.
  • The section below explains how to create, initialize, and iterate through lists. You may also see examples of a few useful list methods.
Did you know? The term “Generic” means allowing to design a class or method where its type is specified at the declaration/initialization time. The Generics were added in the C# language version 2.0.

How to create/declare a List<T> in C#?

The syntax for creating a List<T>:

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

In the above syntax, we created a list namely list_name of the type int. Similarly, you may create a list of string as follows:

List<string> list_string = new List<string>();

Initializing a list

You may use the add method of list class for adding new elements to the list after its declaration. However, you may also create elements at the time of declaration in the list.

For example, we have a List of type string with three elements at the time of declaration as follows:

List<string> str_list = new List<string>(new string[] { “string1”, “string2”, “string3” });

The following simple code shows declaring, initializing, and accessing a list element:

The output of the above example should be:


As such, the index of a list starts at zero.

Note the System.Collections.Generic; as the second line of code. You have to include this in order to work with Lists.

Adding elements to a list by Add method

In the example of List Add method, I have created a list of int type. This is followed by using the Add method for adding five elements into the list. The Add method inserts a new element at the end of List <T>.

Finally, I used the foreach statement for iterating through the list and displayed the List<T> elements:

The output:

c# list

How to insert an item at the specified index by Insert method

Rather than adding an element at the end of a List<T> but at the specified index, you may use the Insert method of the List<T> class.

The general syntax of using the Insert method is:

public void Insert (int index, T item);

In this example, a List<T> of int items is created initially with five elements. Then a foreach statement is used to display the List<T>.

This is followed by using the Insert method and I added a new element at the 3 index (starting at 0). Again, I used the foreach and displayed the list elements and you can see the order of new element:

c# list-insert

The number 13 is displayed at 3 index.

The example of adding multiple items by InsertRange method

If you have a number of elements to add from another collection to an existing list then you may use the InsertRange method.

The syntax for using the InsertRange method is:

public void InsertRange (int index, System.Collections.Generic.IEnumerable<T> collection);

So, you may specify the index position where you want to start inserting new elements from another collection.

To demonstrate that, the following example creates a list and an array. After creating the array, its elements are added to the list as shown in the example below:

The output of the above program:

c# list insertRange

An example of sorting a List<T> by sort method

If you have a list which items are in random order and you want it to sort it then use the List<T> class sort() method.

For using the sort method, simply use the list name that you want to sort followed by the sort() keyword.

In the following example, we have a random list of integer elements. The list items are displayed before and after using the sort() method. Have a look at the code and output:

The result:

c# list-sort

How to remove a List<T> item? The Remove method

The Remove() method deletes the first occurrence of the specified object from the List<T>.  The syntax of using the Remove method is:

public bool Remove (T item);

If the item is found, the Remove method returns true, otherwise false.

See the following example where we have a list of following numbers:

[1, 5, 11, 21, 5, 15]

The Remove() method is used to delete the element 5 from the list. See the code and result:

The Result:

c# list Remove

You can see, the List<T> contains 5 two times. However, the Remove() method deleted only the first occurrence.

Removing all occurrences of the specified element by RemoveAll method

The example below shows using the RemoveAll method for deleting all occurrences of the item in the specified List<T>. I am using the same list items as in the above example i.e. occurrences of item 5 two times. See the output before and after using the RemoveAll method:

The Output:

c# list-RemoveAll

The output shows the occurrence of 5 is removed twice.

Using Clear method for removing all elements in a List<T>

If you require removing all elements from the list in the single call then use the Clear() method. The Clear() method is simple to use i.e.:


The example below shows a list before and after using the clear method:

c# list-Clear

You can see the list is empty after using the Clear() method.

How to search in C# List<T>?

For searching element(s) in lists, you may use a number of methods. The following methods are available:

  • Contains
  • Find
  • Exists

Each of these methods is explained below with an example.

The example of Contains method

The Contains method is used to determine if an item exists in the list or not. It returns true if the item exists and false if it does not.

The following simple example shows how to use the Contains method:

The output of the above code is:

The item Tutorials exists in the List<T>

Note that the Contains method performs searching as case sensitive. If we used the word “tutorials” the returned value should have been false.

How to use Find method of List<T>?

The general syntax for using the Find method is:

public T Find (Predicate<T> match);

By using the Find method, you may search a list for items based on the condition that is defined by the specified predicate.

The Find method returns the first occurrence in the entire List<T>. For showing how it works, see the example below.

We have a string list of six items. The fruit “Mango” is used two times in the List<T>. See how the Find method is used with lambda expression and the result:

The result:


Learn more about the Find method here.

Using Exists method for searching element(s) in List<T>

You may also use the Exists method for determining if one or more elements exist in the List<T> by giving the condition defined by the specified predicate. The general syntax for using the Exists method is:

public bool Exists (Predicate<T> match);

The following example shows using the Exists method:

The output of the code:



This div height required for enabling the sticky sidebar