Get List Length in C#

Get List Length in C#

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; 


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.


Total Number of students: 7

Related Article - Csharp List

  • Convert an IEnumerable to a List in C#
  • C# Remove Item From List
  • C# Join Two Lists Together
  • Get the First Object From List<Object> Using LINQ
  • Find Duplicates in a List in C#
  • The AddRange Function for List in C#