How to find the size of a list in C#?

A List in C# is a dynamic collection that can grow or shrink in size. To determine the size of a list, you can use two different properties: Count (for the number of actual elements) and Capacity (for the total allocated space).

Syntax

To get the number of elements in a list −

listName.Count

To get the total capacity (allocated space) of a list −

listName.Capacity

Understanding Count vs Capacity

Count vs Capacity Count Number of actual elements in the list Count = 3 Capacity Total allocated space in memory Capacity = 4 Capacity ? Count (always)

Using Count Property

The Count property returns the actual number of elements in the list −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(string[] args) {
      var products = new List<string>();
      products.Add("Accessories");
      products.Add("Clothing");
      products.Add("Footwear");
      
      Console.WriteLine("Our list....");
      foreach(var p in products) {
         Console.WriteLine(p);
      }
      Console.WriteLine("Number of elements = " + products.Count);
   }
}

The output of the above code is −

Our list....
Accessories
Clothing
Footwear
Number of elements = 3

Using Capacity Property

The Capacity property returns the total number of elements the list can hold before resizing −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(string[] args) {
      var products = new List<string>();
      products.Add("Accessories");
      products.Add("Clothing");
      products.Add("Footwear");
      
      Console.WriteLine("Our list....");
      foreach(var p in products) {
         Console.WriteLine(p);
      }
      Console.WriteLine("Count = " + products.Count);
      Console.WriteLine("Capacity = " + products.Capacity);
   }
}

The output of the above code is −

Our list....
Accessories
Clothing
Footwear
Count = 3
Capacity = 4

Comparison

Property Purpose Use Case
Count Returns actual number of elements Most commonly used for list size
Capacity Returns allocated memory space Used for memory optimization

Setting Initial Capacity

You can set the initial capacity to optimize memory usage −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(string[] args) {
      var numbers = new List<int>(10);
      numbers.Add(1);
      numbers.Add(2);
      numbers.Add(3);
      
      Console.WriteLine("Count = " + numbers.Count);
      Console.WriteLine("Capacity = " + numbers.Capacity);
   }
}

The output of the above code is −

Count = 3
Capacity = 10

Conclusion

Use Count to get the actual number of elements in a list, which is the most common requirement. Use Capacity when you need to understand memory allocation or optimize performance by setting initial capacity.

Updated on: 2026-03-17T07:04:35+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements