List in C# is a Generic collection class that allows you to access its items by index. List<T> represents a list of strongly typed objects, that means you can explicitly define the type of object the list can hold.

In C# List has properties similar to an array. however, List automatically expands as the number of elements increases, where as size of an array remains fixed.

Create a List in C#

You need to specify a type parameter while creating a List<T>. The following example shows that.

In above example.

  • List<string> lstCountry = new List<string>();  creates a list that holds only string values.
  • var lstNumbers = new List<int>(); creates a list that holds only int values.

Add item to a List in C#

You can use Add() method to add new item to a List<T> of same type.

In above, we are adding string items to list lstCountry and int values to list lstNumbers using Add() method. It gives compile error if you add different types of item in to the list.

Create a List using object initializer syntax

You can also use the object initializer syntax to create a strongly typed list object and add elements to it in a single statement. In below example, we are creating list using object initializer syntax.

Add list of Items in to another List in C#

You can use the AddRange(IEnumerable<T> collection) method to add list of items or an array of items in to another List<T>, lets have a look at below example.

Here in above,

  • lstCountry.AddRange(new List<string>() { "Poland", "Greece", "Germany" }); adds a new list of string items to lstCountry.
  • lstCountry.AddRange(new string[] { "Italy", "Brazil", "Peru", "Canada" }); adds an array of string items to lstCountry.

Remove an item from a List in C#

We have various methods available to remove an item from a List<T>

  • bool Remove(T item) : This method removes the first occurrence of the specified element.
  • void RemoveAt(int index) : This method removes an element from the specified index.
  • void RemoveRange(int index, int count) : This method removes a range of elements from the List, here index value is the starting point and count is the number of items to be removed.

You can use the Remove() method to remove the first occurrence of the specified element from the List and use RemoveAt() method to remove an element from the specified index.

In above, this lstCountry.RemoveAt(1); removes “United States” from the list, we can also use Remove() method to remove specific item from the list, for example  lstCountry.Remove("United States");

Use of RemoveRange() removes a range of items from the list, here we have used start index as 0 and number of elements removed as 2, which means “India” and “United States” will be removed from the list.

Read items in a List in C#

We can access or read elements in List using the index operator and for-each loop, C# is a zero-index based, hence index starts from 0. Lets have a look at below example.

When we run above code then it produces below output.

List output 1.0

Complete Example Sample :

When we run above code then it produces below output.

List output 1.1

Important methods and properties of List<T> type in C#

List<T>.Count : It is a property , used to get the number of elements present in a List<T>

List<T>.Clear() : It is a method , used to remove all from a List<T>

List<T>.Contains() : It is a method, that checks whether a specified item exists or not in a List<T>, it returns true if it exists or else false

List<T>.Insert() :  Inserts an element at the specified index in a List<T>. It takes an index and value to insert.

List<T>.Find() : It is a method, that finds first and single element based on the specified predicate function or a condition.

There are couple of other methods that are basically used for sorting and searching a List<T> , We will cover that in next article.

Thanks for your time, feel free to provide your feedback and do check my other blogs on Generics and Var Type in C#.