ZetCode

C# List Count

last modified October 29, 2022

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

C# list is a collection of elements of the same type. The elements can be accessed by index.

To count elements, we can use the Count property or the Enumerable.Count extension method.

C# List Count simple example

We count the number of elements with the Count property and method.

Program.cs
var words = new List<string> { "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");

The program defines a list of strings. We print the number of elements of the list to the console twice.

$ 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
var words = new List<string> { "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
var cars = new List<Car>
{
    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 the next program, we arrange elements into groups and count the number of elements in each group.

Program.cs
var cars = new List<Car>
{
    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 { Key = g.Key, Count = g.Count() };

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

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

We have a list of cars. We group them by their colours and count each group's size.

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

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

List all C# tutorials.