C# List – Using Lists in C#

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.

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize an empty list named "name_list"
  10. List<string> name_list = new List<string>();
  11. //Add elements to the list
  12. name_list.Add("Roland");
  13. name_list.Add("Jonh");
  14. name_list.Add("James");
  15. foreach (string element in name_list)
  16. {
  17. Console.WriteLine("Name: {0}", element);
  18. }
  19. }
  20. }
  21. }

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.

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize an empty list named "name_list"
  10. List<string> name_list = new List<string>();
  11. //Add elements to the list
  12. name_list.Add("Roland");
  13. name_list.Add("John");
  14. name_list.Add("James");
  15. //Dispay the list elements
  16. Console.WriteLine("Before removing");
  17. foreach (string element in name_list)
  18. {
  19. Console.WriteLine("Name: {0}", element);
  20. }
  21. //Remove "James" from the list
  22. name_list.Remove("James");
  23. //Dispay the list elements after removing
  24. Console.WriteLine("------");
  25. Console.WriteLine("After removing");
  26. foreach (string element in name_list)
  27. {
  28. Console.WriteLine("Name: {0}", element);
  29. }
  30. }
  31. }
  32. }

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.

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize an empty list named "name_list"
  10. List<string> name_list = new List<string>();
  11. //Add elements to the list
  12. name_list.Add("Roland");
  13. name_list.Add("John");
  14. name_list.Add("James");
  15. //Dispay the list elements
  16. foreach (string element in name_list)
  17. {
  18. Console.WriteLine("Name: {0}", element);
  19. }
  20. //
  21. Console.WriteLine("---------------------------------------------");
  22. Console.WriteLine("Get the index of an element in the list.");
  23. Console.WriteLine("---------------------------------------------");
  24. string name = "James";
  25. int index = name_list.IndexOf(name);
  26. Console.WriteLine("The index of {0} is the list is {1}", name, index);
  27. }
  28. }
  29. }

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.

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize an empty list named "list"
  10. List<string> list = new List<string>();
  11. //Add elements to the list
  12. list.Add("D");
  13. list.Add("A");
  14. list.Add("X");
  15. list.Add("T");
  16. list.Add("M");
  17. list.Add("J");
  18. list.Add("F");
  19. //Dispay the list elements before sorting
  20. Console.WriteLine("-----------------------");
  21. Console.WriteLine("Before sorting");
  22. Console.WriteLine("-----------------------");
  23. foreach (string element in list)
  24. {
  25. Console.WriteLine("Name: {0}", element);
  26. }
  27. //Sort the list
  28. list.Sort();
  29. //Dispay the list elements after sorting
  30. Console.WriteLine("-----------------------");
  31. Console.WriteLine("After sorting");
  32. Console.WriteLine("-----------------------");
  33. foreach (string element in list)
  34. {
  35. Console.WriteLine("Name: {0}", element);
  36. }
  37. //
  38. }
  39. }
  40. }

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.

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize an empty list named "list"
  10. List<string> list = new List<string>();
  11. //Add elements to the list
  12. list.Add("D");
  13. list.Add("A");
  14. list.Add("X");
  15. list.Add("T");
  16. list.Add("M");
  17. list.Add("J");
  18. list.Add("F");
  19. //Dispay the list elements
  20. Console.WriteLine("-----------------------");
  21. foreach (string element in list)
  22. {
  23. Console.WriteLine("Element: {0}", element);
  24. }
  25. //Check if "J" is included in the list.
  26. string check_element = "J";
  27. CheckIfElementIncludedInTheList(list, check_element);
  28. check_element = "Z";
  29. //Check if "Z" is included in the list.
  30. CheckIfElementIncludedInTheList(list, check_element);
  31. }
  32. static void CheckIfElementIncludedInTheList(List<string> list, string element)
  33. {
  34. if (list.Contains(element))
  35. {
  36. Console.WriteLine("The element {0} is included in the list.", element);
  37. }
  38. else
  39. {
  40. Console.WriteLine("The element {0} is not included in the list.", element);
  41. }
  42. }
  43. }
  44. }

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:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize an empty list named "list"
  10. List<string> list = new List<string>();
  11. //Add elements to the list
  12. list.Add("D");
  13. list.Add("A");
  14. list.Add("X");
  15. list.Add("T");
  16. list.Add("M");
  17. list.Add("J");
  18. list.Add("F");
  19. //Dispay the list elements
  20. Console.WriteLine("-----------------------");
  21. foreach (string element in list)
  22. {
  23. Console.WriteLine("Element: {0}", element);
  24. }
  25.  
  26. string findingElement = "M";
  27. FindElement(list, findingElement);
  28. findingElement = "W";
  29. FindElement(list, findingElement);
  30. }
  31. static void FindElement(List<string> list, string element)
  32. {
  33. var found = list.Find(x => x == element);
  34. if (found != null)
  35. {
  36. int index = list.IndexOf(found);
  37. Console.WriteLine("The element {0} is found! The index of the element is {1}",
  38. element, index);
  39. }
  40. else
  41. {
  42. Console.WriteLine("The element {0} is not found!", element);
  43. }
  44. }
  45. }
  46. }

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.


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables