7 Examples to Learn C# list and its Methods – Sort, Find etc

What is the List in C#?

The list is a collection of elements or variables in a specified order. The difference between array and list is that the list is dynamically sized whereas arrays are fixed.

A few main points about the C# list are:

  • A list class of C# is a collection of variables.
  • The list <T> is defined in the System.Collections.Generic namespace.
  • The list <T> is the strongly typed list of objects.
  • The T in the list <T> defines the type of elements in the list like integers, strings etc.
  • The elements of list C# are accessible by the integer-based index.
  • Just like arrays, the index of the list starts at 0.
  • The list is useful when you have no idea that how many elements an array will contain.
  • The list class provides methods and properties to add, remove, sort elements in the list.
  • A very large list <T> object, in the 64-bit system, can contain as much as 2 billion elements.

How to create a C# list?

This is how you can create an integer list:

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

As we have the basic idea what list are and how to create it, now let us look at a few examples of using the list in the C# programs. We will also look at a few useful methods of the list after examples.

A list example with integer elements

In this example, we will create a list with integer elements. We will create four elements and then display it by using a for loop. See code and output by clicking the link below:

You can see, four elements are shown when we run this example by pressing F5 (in our console based created project). Two methods of the list, add and count are used in the example.

The C# list add method is used to add elements in the list. While C# count property is used in the for loop to get the total count of elements in the list, that acts as the maximum value in the for loop. The count list property returns the total count of elements in the list.

A list with string example

Following is an example of creating a list of string elements. Again we have created a four elements list that will be displayed by using the for loop. See example and output by clicking the link or image below:

You can see, the list elements are added by using the add list method. After creating the elements, we used a for loop and count method to display the list elements.

The list insert method to add element example

You can use list insert method to add an element at a specific position. Following example shows how to do that:

You can see, a list is created with four elements. After displaying list elements by using a for loop, we added an element at the index 3 by using the insert method. The list is displayed again with added elements.

Accessing specific element in the list example

You can access a specific element by using the integer-based index number starting from zero. Following example shows that how to access a specific element in the list.

The list with the foreach loop example

In above examples, we used for loop to display items of the list. You can use a foreach loop to iterate through the list as well. Following is an example of the C# list with the foreach loop example. See code and output by clicking the link below:

The list sort example

Following is a list sort example. The sort method will sort the list elements. For this example, we have created a list with unordered element values.

First, we displayed the list elements in the original order. After that, we used the list sort method and displayed the list again by using the foreach loop.

Note that, the sort method will sort the list from lower to the higher number for numeric list while sort method will sort alphabetically for the string list.

C# list Find method example

The list Find method is used to search in the list. The Find method returns the first occurrence of the matched condition in the given list. See the following example of using list find

See the following example of using list find method where we have four elements list. After that, we searched the list by using the list find method to return element greater than the value of 10.

See the code and output:

Further Reading: Array of C# | The ArrayList


Was this article helpful?

Related Articles

Leave A Comment?