List in C# is a Generic collection class that allows to access its items by index. List<T> represents a list of strongly typed objects, where T is the data type. List<T> 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 :

While using object initializer syntax , we can also create a strongly typed list of object and add elements to it in a single statement.

In below example, we are creating a 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 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.

Lets have a look at below example.

In above, the statement lstCountry.RemoveAt(1); removes “United States” , which is at index 1 from the list.

We can also use Remove() method to remove a specific item from the list, for example lstCountry.Remove("United States");

We can use RemoveRange() to remove a range of items from the list. lets have a look at below example.

In above example, we have used start index as 0 and number of elements removed as 2 , which removes “India” and “United States” 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.

Lets have a look at below example.

Find the output as below :

List output 1.0

Example :

Find the output as below :

List output 1.1

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

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

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

List<T>.Contains() : This method checks whether a specified item exists or not in a List<T>, it returns true if item exists or else returns 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#.