ZetCode

C# List Count

last modified November 6, 2026

This article provides a guide on counting elements in a C# list, explaining different methods to achieve this efficiently.

In C#, a List is a dynamic collection that stores elements of the same type. Each element is indexed, allowing direct access through its position within the list. Unlike arrays, lists offer flexibility in size, making them useful for various data management scenarios.

To determine the number of elements in a list, we can use two approaches:

Both methods provide efficient ways to retrieve the list size, depending on the use case and whether filtering is required.

C# List Count Simple Example

In this example, we demonstrate two approaches for counting elements in a C# list—the Count property and the Count() LINQ extension method.

Program.cs
List<string> words = [ "sky", "cup", "new", "war", "wrong",
    "crypto", "forest", "water" ];

Console.WriteLine($"There are {words.Count} elements in the list");
Console.WriteLine($"There are {words.Count()} elements in the list");

Both lines output the total number of elements in the list. The Count property provides direct access to the element count, while Count() from LINQ is useful when applying filtering conditions. For a list, the Count() method without a predicate does not iterate the elements; it detects that the source is a collection and reads its Count property.

$ dotnet run
There are 8 elements in the list
There are 8 elements in the list

C# List Count with predicate

A predicate is a function which returns a boolean value. In the next example, we count the number of elements that satisfy the given predicate.

Program.cs
List<string> words = [ "sky", "cup", "new", "war", "wrong",
    "crypto", "forest", "water" ];

var n = words.Where(e => e.StartsWith('w')).Count();
Console.WriteLine($"{n} words start with w");

var n2 = words.Count(e => e.StartsWith('c'));
Console.WriteLine($"{n2} words start with c");

The program counts the number of words starting with 'w' and 'c'.

var n = words.Where(e => e.StartsWith('w')).Count();

In the first case, we use the Where method to filter the list and then we call Count on the result.

var n2 = words.Count(e => e.StartsWith('c'));

Alternatively, we can pass a lambda expression as a parameter to Count.

$ dotnet run
3 words start with w
2 words start with c

C# List Count with query expression

In the next program, we count elements with LINQ's query expression.

Program.cs
List<Car> cars =
[
    new ("Audi", 52642),
    new ("Mercedes", 57127),
    new ("Skoda", 9000),
    new ("Volvo", 29000),
    new ("Bentley", 350000),
    new ("Citroen", 21000),
    new ("Hummer", 41400),
    new ("Volkswagen", 21600),
];

var n = (from car in cars
          where car.Price > 30000 && car.Price < 100000
          select car).Count();

Console.WriteLine(n);

record Car(string Name, int Price);

In the program, we find out all cars with price between 30000 and 100000 and count them.

$ dotnet run
3

C# List Count Groups

In this example, we use LINQ to group elements in a C# list based on a common attribute and count the number of items in each group.

The program defines a list of cars, each represented by a name, color, and price. The cars are grouped by their color, and the total number of cars in each group is determined using the Count method.

Program.cs
List<Car> cars =
[
    new ("Audi", "red", 52642),
    new ("Mercedes", "blue", 57127),
    new ("Skoda", "black", 9000),
    new ("Volvo", "red", 29000),
    new ("Bentley", "yellow", 350000),
    new ("Citroen", "white", 21000),
    new ("Hummer", "black", 41400),
    new ("Volkswagen", "white", 21600),
];

var groups = from car in cars
             group car by car.Colour into g
             select new { g.Key, Count = g.Count() };

foreach (var group in groups)
{
    Console.WriteLine($"{group.Key}: {group.Count}");
}

record Car(string Name, string Colour, int Price);

Each unique color serves as a grouping key, and the Count method retrieves the number of cars within each category. This technique is particularly useful for summarizing categorized data and performing aggregation tasks efficiently.

$ dotnet run
red: 2
blue: 1
black: 2
yellow: 1
white: 2

Count and Capacity

Count is the number of elements in the list. Capacity is the size of the internal array that stores them.

Program.cs
var vals = new List<int>();
Console.WriteLine($"Count: {vals.Count}, Capacity: {vals.Capacity}");

for (int i = 1; i <= 5; i++)
{
    vals.Add(i);
    Console.WriteLine($"Count: {vals.Count}, Capacity: {vals.Capacity}");
}

vals.TrimExcess();
Console.WriteLine($"After TrimExcess: Count {vals.Count}, Capacity {vals.Capacity}");

var big = new List<int>(1000);
Console.WriteLine($"Preallocated: Count {big.Count}, Capacity {big.Capacity}");

An empty list has no internal array. When the array is full, the list allocates a new one with double the capacity and copies the elements.

vals.TrimExcess();

TrimExcess shrinks the capacity to the number of elements. When we know the number of elements in advance, passing the capacity to the constructor avoids the repeated reallocations.

$ dotnet run
Count: 0, Capacity: 0
Count: 1, Capacity: 4
Count: 2, Capacity: 4
Count: 3, Capacity: 4
Count: 4, Capacity: 4
Count: 5, Capacity: 8
After TrimExcess: Count 5, Capacity 5
Preallocated: Count 0, Capacity 1000

Counting without enumeration

For a lazy sequence, Count() must enumerate all elements. The TryGetNonEnumeratedCount method returns the count only if it is available cheaply.

Program.cs
List<int> list = [1, 2, 3, 4];
IEnumerable<int> query = list.Where(e => e % 2 == 0);
IEnumerable<int> generated = Generate();

Console.WriteLine($"list: {Try(list)}");
Console.WriteLine($"query: {Try(query)}");
Console.WriteLine($"generated: {Try(generated)}");

Console.WriteLine($"Any: {list.Any()}, Count() > 0: {list.Count() > 0}");

static string Try(IEnumerable<int> items) =>
    items.TryGetNonEnumeratedCount(out int n) ? n.ToString() : "unknown without enumeration";

static IEnumerable<int> Generate()
{
    yield return 1;
    yield return 2;
}

The method succeeds for the list, but not for a LINQ query or an iterator method, whose elements are produced on demand.

Console.WriteLine($"Any: {list.Any()}, Count() > 0: {list.Count() > 0}");

To check whether a sequence has any elements, use Any; it stops after the first element, while Count() on a lazy sequence goes through all of them.

$ dotnet run
list: 4
query: unknown without enumeration
generated: unknown without enumeration
Any: True, Count() > 0: True

Source

List Count property

In this article we have shown how to count list elements in C#.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all C# tutorials.