C# Programming Language: Lists
Hi everyone, welcome back. In this tutorial, we will be going over Lists in C#. Lists can be used to store a collection of data, similar to that of an array. The key difference between a List and an array is that a List is resizable and an array is not. This leaves the List as a great option when wanting to modify a collection of data. With this introduction out of the way, let’s get into it.

Creating List
Let’s start by creating a List. Before we can create the List, we need to make sure we have our List import, which is included within the System.Collections.Generic package:
using System.Collections.Generic;
Now, we can successfully create our List:
List<int> myList = new List<int>();
So now, we have successfully created our List variable of the integer type named myList. When we create our List, we need to specify the data type. As we can see in the example above, we have used the ‘int’ data type. We can use other data types as well, such as the string or boolean data types.
Adding Elements Into The List
Now, let’s try to add elements into our List. We can do this by using the built in Add() function that is provided. Let’s see an example:
List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
Console.WriteLine(String.Join(" ", myList));Output:
1 2 3
In the example above, we can see that we added a few different numbers/integers into our List by using the Add() function. We can verify that they were successfully added by printing our List and checking the output.
Updating an Element in The List
Now, let’s see how we can update an already existing element within our List. We can do this by using the index position of the value we wish to update. Let’s see an example:
List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList[1] = 4;
Console.WriteLine(String.Join(" ", myList));Output:
1 4 3
In the example above, we can see that we have added a few numbers into our List. Then we set the value at the index position of 1 to be ‘4.’ We can verify that the value at index position 1 was updated to ‘4’ by printing our List and checking the output.
Inserting Elements Into The List
Now, let’s see how we can add an element into the middle of an already existing List. We can do this by using the built in Insert() function. We will need to specify the index position of the value we want to add. Let’s see an example:
List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Insert(0, 4);
Console.WriteLine(String.Join(" ", myList));Output:
4 1 2 3
In the example above, we can see that we have added a few numbers into our List. Then we used the Insert() function to add the number ‘4’ at position 0, which is the very first position. We can verify that it was successfully added by printing our List and checking the output.
Removing Elements by Value From a List
Now, let’s see how we can remove an element by using its value from a List. We can do this by using the built in Remove() function. Let’s see an example:
List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Remove(2);
Console.WriteLine(String.Join(" ", myList));Output:
1 3
In the example above, we can see that we added a few numbers into our List. Then we used the Remove() function to remove the value of ‘2’ from our List. We can verify that it was successfully removed by printing our List and checking the output.
Removing Elements by Index From a List
Now, let’s see how we can remove an element by using its index position from a List. We can do this by using the built in RemoveAt() function. Let’s see an example:
List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.RemoveAt(2);
Console.WriteLine(String.Join(" ", myList));Output:
1 2
In the example above, we can see that we added a few numbers into our List. Then we used the RemoveAt() function to remove the value at index position ‘2’ from the List. In this case, our ‘3’ is the value at the corresponding index position. We can verify that it was successfully removed by printing our List and checking the output.
Clearing a List
Now, let’s see how we can remove all elements from a List. We can do this by using the built in Clear() function. Let’s see an example:
List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Clear();
Console.WriteLine(String.Join(" ", myList));Output:
In the example above, we can see that we added a few numbers into our List. Then we used the Clear() function to remove all elements from the List. We can verify that it was successful by printing our List and checking our output. Our output should be empty because we removed everything.
Checking Elements in a List
Now, let’s see how we can check to see if a specific value is within a List. We can do this by using the built in Contains() function. We will need to specify which value to check for. Let’s see an example:
List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
Console.WriteLine(myList.Contains(2));
Console.WriteLine(myList.Contains(4));Output:
True
False
In the example above, we can see that we added a few numbers into our List. Then we printed out the results of the Contains() function. We checked to see if our List contained the value ‘2’ and the value ‘4.’ As we can see in the output, our List contains a value of 2, but not 4.
Checking the Size of a List
Now, let’s see how we can check the size of the List, or how many elements are in the List. We can do this by using the Count property. Let’s see an example:
List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
Console.WriteLine(myList.Count);Output:
3
In the example above, we can see that we added a few numbers into our List. We then printed out the result of the count on our List. As we can see in the output, our List contains 3 elements.
Conclusion
That is it for the C# List examples. We covered how to create Lists along with some of the basic functions regarding them. I hope this helps. If there are any questions or comments, please let me know. Thanks for reading.