C# List Length – Get the Length of a List

How to get the length of a list in C#

In this tutorial, you will learn how to get the list length and how to use it in combination with other list methods in C#.

Sometimes it is necessary to know the number of items in a list.
Once the length of the list is known, it can be used in other list methods such as Remove() and Insert().

More C# tutorials:

To get the length of a list, use the List count method. The following is its syntax:

List.Count

Example:

  1. //Initialize an empty list
  2. List<string> countries = new List<string>();
  3. //Add items to the list
  4. countries.Add("United State of America");
  5. countries.Add("Japan");
  6. countries.Add("France");
  7. countries.Add("Italy");
  8. //Get the list length
  9. int listLength = countries.Count;
  10. //Write to the console
  11. Console.WriteLine("The list length: {0}", listLength);

More examples of C# list length usage

Example 1

The following sample program shows an example of obtaining the list length:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. //Initialize an empty list
  10. List<string> countries = new List<string>();
  11. //Add items to the list
  12. countries.Add("United State of America");
  13. countries.Add("Japan");
  14. countries.Add("China");
  15. countries.Add("France");
  16. countries.Add("Italy");
  17. //Get the list length
  18. int listLength = countries.Count;
  19. //Write to the console
  20. Console.WriteLine("The list length: {0}", listLength);
  21. }
  22. }
  23. }

Output:

The list length: 5

Example 2

The following sample program shows an example of retrieving the second item from the end of the list:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. //Initialize an empty list
  10. List<int> numbers = new List<int>();
  11. //Add items to the list
  12. numbers.Add(101);
  13. numbers.Add(299);
  14. numbers.Add(339);
  15. numbers.Add(222);
  16. numbers.Add(999);
  17. //Get the list length
  18. int listLength = numbers.Count;
  19. //Get the second last item
  20. int secondLastItem = numbers[listLength - 2];
  21. //Write to the console
  22. Console.WriteLine("The second last item: {0}", secondLastItem);
  23. }
  24. }
  25. }

Output:

The second last item: 222

Example 3

The following sample program illustrates the use of list length and the Remove() method to remove the last three items from a list:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. //Initialize an empty list
  10. List<string> countries = new List<string>();
  11. //Add items to the list
  12. countries.Add("Japan");
  13. countries.Add("France");
  14. countries.Add("China");
  15. countries.Add("Italy");
  16. countries.Add("Spain");
  17. countries.Add("United State of America");
  18. //Get the list length
  19. int listLength = countries.Count;
  20. Console.WriteLine("**************************");
  21. //Write to the console
  22. Display(countries);
  23. Console.WriteLine("The list length: {0}", listLength);
  24. //Remove the last three items from the list
  25. countries.Remove(countries[listLength - 1]);
  26. countries.Remove(countries[listLength - 2]);
  27. countries.Remove(countries[listLength - 3]);
  28. //
  29. listLength = countries.Count;
  30. Console.WriteLine("**************************");
  31. Console.WriteLine("After removing:");
  32. Display(countries);
  33. Console.WriteLine("The list length: {0}", listLength);
  34. }
  35. static void Display(List<string> list)
  36. {
  37. foreach (string item in list)
  38. {
  39. Console.WriteLine("{0}", item);
  40. }
  41. }
  42. }
  43. }

Output:

Japan
France
China
Italy
Spain
United State of America
The list length: 6
**************************
After removing:
Japan
France
China
The list length: 3

Example 4

The following sample program illustrates the use of list length and the Insert() method to add a new item at the second position from the end of the list:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. //Initialize an empty list
  10. List<string> countries = new List<string>();
  11. //Add items to the list
  12. countries.Add("France");
  13. countries.Add("Japan");
  14. countries.Add("United State of America");
  15. countries.Add("Italy");
  16. countries.Add("China");
  17. countries.Add("Spain");
  18. //Get the list length
  19. int listLength = countries.Count;
  20. Console.WriteLine("**************************");
  21. //Write to the console
  22. Display(countries);
  23. Console.WriteLine("The list length: {0}", listLength);
  24. //Insert a new item at the second position from the end of the list.
  25. countries.Insert(listLength - 2, "Sweden");
  26. //
  27. listLength = countries.Count;
  28. Console.WriteLine("**************************");
  29. Console.WriteLine("After inserting:");
  30. Display(countries);
  31. Console.WriteLine("The list length: {0}", listLength);
  32. }
  33. static void Display(List<string> list)
  34. {
  35. foreach (string item in list)
  36. {
  37. Console.WriteLine("{0}", item);
  38. }
  39. }
  40. }
  41. }

Output:

France
Japan
United State of America   
Italy
China
Spain
The list length: 6        
**************************
After inserting:
France
Japan
United State of America   
Italy
Sweden
China
Spain
The list length: 7

Summary

In this C# tutorial, you learned how to get the length of a list and how to use it in conjunction with other list methods such as Insert() and Remove(). Once the length of the list is determined, it can be used for a variety of purposes.


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