What is a List in C#?
C# has a List<T> data structure for handling values of the same data type together.
Unlike arrays, Lists allow elements to be easily added or removed. Therefore, if the number of elements changes, you should use List<T>.
A "List" is a collection of the same data type's values.
Lists, unlike arrays, allow you to simply add and remove elements. As a result, a List is used when the number of elements changes.
Learn more:
How to add an element to a list
To add an element to a list, you can use the Add method.
using System; using System.Collections.Generic; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize an empty list named "name_list" List<string> name_list = new List<string>(); //Add elements to the list name_list.Add("Roland"); name_list.Add("Jonh"); name_list.Add("James"); foreach (string element in name_list) { Console.WriteLine("Name: {0}", element); } } } }
Output:
Name: Roland
Name: John
Name: James
See the article below for a detailed explanation of how to use the Add method to add an element to a list.
Add an element to a list in C#
How to delete an element from a list
The Remove method is used to delete elements from a list.
using System; using System.Collections.Generic; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize an empty list named "name_list" List<string> name_list = new List<string>(); //Add elements to the list name_list.Add("Roland"); name_list.Add("John"); name_list.Add("James"); //Dispay the list elements Console.WriteLine("Before removing"); foreach (string element in name_list) { Console.WriteLine("Name: {0}", element); } //Remove "James" from the list name_list.Remove("James"); //Dispay the list elements after removing Console.WriteLine("------"); Console.WriteLine("After removing"); foreach (string element in name_list) { Console.WriteLine("Name: {0}", element); } } } }
Output:
Before removing
Name: Roland
Name: John
Name: James
------
After removing
Name: Roland
Name: John
The following article explains in detail how to remove elements with Remove.
Remove an element from a list in C#
How to find the index of an element in a list
You can use the IndexOf method to search for an element in a List and obtain its index.
using System; using System.Collections.Generic; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize an empty list named "name_list" List<string> name_list = new List<string>(); //Add elements to the list name_list.Add("Roland"); name_list.Add("John"); name_list.Add("James"); //Dispay the list elements foreach (string element in name_list) { Console.WriteLine("Name: {0}", element); } // Console.WriteLine("---------------------------------------------"); Console.WriteLine("Get the index of an element in the list."); Console.WriteLine("---------------------------------------------"); string name = "James"; int index = name_list.IndexOf(name); Console.WriteLine("The index of {0} is the list is {1}", name, index); } } }
How to sort the List
The linked article explains how to sort the List, using the Sort() method to sort the elements of a List.
using System; using System.Collections.Generic; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize an empty list named "list" List<string> list = new List<string>(); //Add elements to the list list.Add("D"); list.Add("A"); list.Add("X"); list.Add("T"); list.Add("M"); list.Add("J"); list.Add("F"); //Dispay the list elements before sorting Console.WriteLine("-----------------------"); Console.WriteLine("Before sorting"); Console.WriteLine("-----------------------"); foreach (string element in list) { Console.WriteLine("Name: {0}", element); } //Sort the list list.Sort(); //Dispay the list elements after sorting Console.WriteLine("-----------------------"); Console.WriteLine("After sorting"); Console.WriteLine("-----------------------"); foreach (string element in list) { Console.WriteLine("Name: {0}", element); } // } } }
Output:
-----------------------
Before sorting
-----------------------
Name: D
Name: A
Name: X
Name: T
Name: M
Name: J
Name: F
-----------------------
After sorting
-----------------------
Name: A
Name: D
Name: F
Name: J
Name: M
Name: T
Name: X
How can you tell if a value is in a list?
The List<T> Contains method is used to search a List element to see if it contains a value.
using System; using System.Collections.Generic; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize an empty list named "list" List<string> list = new List<string>(); //Add elements to the list list.Add("D"); list.Add("A"); list.Add("X"); list.Add("T"); list.Add("M"); list.Add("J"); list.Add("F"); //Dispay the list elements Console.WriteLine("-----------------------"); foreach (string element in list) { Console.WriteLine("Element: {0}", element); } //Check if "J" is included in the list. string check_element = "J"; CheckIfElementIncludedInTheList(list, check_element); check_element = "Z"; //Check if "Z" is included in the list. CheckIfElementIncludedInTheList(list, check_element); } static void CheckIfElementIncludedInTheList(List<string> list, string element) { if (list.Contains(element)) { Console.WriteLine("The element {0} is included in the list.", element); } else { Console.WriteLine("The element {0} is not included in the list.", element); } } } }
Output:
Element: D
Element: A
Element: X
Element: T
Element: M
Element: J
Element: F
The element J is included in the list.
The element Z is not included in the list.
How to search for elements by specifying conditions
You can use the Find() method to search for elements in the List<T> and retrieve elements that match the criteria.
Here is an example:
using System; using System.Collections.Generic; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize an empty list named "list" List<string> list = new List<string>(); //Add elements to the list list.Add("D"); list.Add("A"); list.Add("X"); list.Add("T"); list.Add("M"); list.Add("J"); list.Add("F"); //Dispay the list elements Console.WriteLine("-----------------------"); foreach (string element in list) { Console.WriteLine("Element: {0}", element); } string findingElement = "M"; FindElement(list, findingElement); findingElement = "W"; FindElement(list, findingElement); } static void FindElement(List<string> list, string element) { var found = list.Find(x => x == element); if (found != null) { int index = list.IndexOf(found); Console.WriteLine("The element {0} is found! The index of the element is {1}", element, index); } else { Console.WriteLine("The element {0} is not found!", element); } } } }
Output:
Element: D
Element: A
Element: X
Element: T
Element: M
Element: J
Element: F
The element M is found! The index of the element is 4
The element W is not found!
Summary
In this tutorial, we have summarized and detailed the various approaches to using a list data structure. Please see the linked article for a detailed explanation of how to use them.