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:
//Initialize an empty list List<string> countries = new List<string>(); //Add items to the list countries.Add("United State of America"); countries.Add("Japan"); countries.Add("France"); countries.Add("Italy"); //Get the list length int listLength = countries.Count; //Write to the console 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:
using System; using System.Collections.Generic; namespace CSharpExample { public class Program { public static void Main(string[] args) { //Initialize an empty list List<string> countries = new List<string>(); //Add items to the list countries.Add("United State of America"); countries.Add("Japan"); countries.Add("China"); countries.Add("France"); countries.Add("Italy"); //Get the list length int listLength = countries.Count; //Write to the console Console.WriteLine("The list length: {0}", listLength); } } }
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:
using System; using System.Collections.Generic; namespace CSharpExample { public class Program { public static void Main(string[] args) { //Initialize an empty list List<int> numbers = new List<int>(); //Add items to the list numbers.Add(101); numbers.Add(299); numbers.Add(339); numbers.Add(222); numbers.Add(999); //Get the list length int listLength = numbers.Count; //Get the second last item int secondLastItem = numbers[listLength - 2]; //Write to the console Console.WriteLine("The second last item: {0}", secondLastItem); } } }
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:
using System; using System.Collections.Generic; namespace CSharpExample { public class Program { public static void Main(string[] args) { //Initialize an empty list List<string> countries = new List<string>(); //Add items to the list countries.Add("Japan"); countries.Add("France"); countries.Add("China"); countries.Add("Italy"); countries.Add("Spain"); countries.Add("United State of America"); //Get the list length int listLength = countries.Count; Console.WriteLine("**************************"); //Write to the console Display(countries); Console.WriteLine("The list length: {0}", listLength); //Remove the last three items from the list countries.Remove(countries[listLength - 1]); countries.Remove(countries[listLength - 2]); countries.Remove(countries[listLength - 3]); // listLength = countries.Count; Console.WriteLine("**************************"); Console.WriteLine("After removing:"); Display(countries); Console.WriteLine("The list length: {0}", listLength); } static void Display(List<string> list) { foreach (string item in list) { Console.WriteLine("{0}", item); } } } }
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:
using System; using System.Collections.Generic; namespace CSharpExample { public class Program { public static void Main(string[] args) { //Initialize an empty list List<string> countries = new List<string>(); //Add items to the list countries.Add("France"); countries.Add("Japan"); countries.Add("United State of America"); countries.Add("Italy"); countries.Add("China"); countries.Add("Spain"); //Get the list length int listLength = countries.Count; Console.WriteLine("**************************"); //Write to the console Display(countries); Console.WriteLine("The list length: {0}", listLength); //Insert a new item at the second position from the end of the list. countries.Insert(listLength - 2, "Sweden"); // listLength = countries.Count; Console.WriteLine("**************************"); Console.WriteLine("After inserting:"); Display(countries); Console.WriteLine("The list length: {0}", listLength); } static void Display(List<string> list) { foreach (string item in list) { Console.WriteLine("{0}", item); } } } }
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.