Get List Length in C#
Fil Zjazel Romaeus Villegas
Dec 20, 2021
Csharp
Csharp List

This tutorial will demonstrate how to get the length of a C# list by using the count function.
A list is a type of collection of objects of a specified type whose values can be accessed by its index.
List<T> myList = new List<T>();
You can add as many items as you want to a list as long as the value’s type that you are adding matches what was defined during initialization.
A C# list has a built-in function Count
that returns the number of items in the list.
int list_count = myList.Count;
Example:
using System;
using System.Collections.Generic;
namespace ListCount_Example
{
class Program
{
static void Main(string[] args)
{
//Initializing the list of strings
List<string> student_list = new List<string>();
//Adding values to the list
student_list.Add("Anna Bower");
student_list.Add("Ian Mitchell");
student_list.Add("Dorothy Newman");
student_list.Add("Nathan Miller");
student_list.Add("Andrew Dowd");
student_list.Add("Gavin Davies");
student_list.Add("Alexandra Berry");
//Getting the count of items in the list and storing it in the student_count variable
int student_count = student_list.Count;
//Printing the result
Console.WriteLine("Total Number of students: " + student_count.ToString());
}
}
}
The list of strings is initialized in the example above, and seven records are added. After adding the records, the number of records is pulled using the count function and stored in an integer value that is then printed.
Output:
Total Number of students: 7