Aggregation Operator: Count

The Count operator returns the number of elements in the collection or number of elements that have satisfied the given condition.

The Count() extension method has the following two overloads:

Count() method overloads in C#

int Count<TSource>();

int Count<TSource>(Func<TSource, bool> predicate);

The first overload method of Count returns the number of elements in the specified collection, whereas the second overload method returns the number of elements which have satisfied the specified condition given as lambda expression/predicate function.

The following example returns the number of elements in a collection using the Count() method in the method syntax.

Example: Count() in C#

IList<Student> studentList = new List<Student>>() { 
        new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
        new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
        new Student() { StudentID = 5, StudentName = "Mathew" , Age = 15 } 
    };

var numOfStudents = studentList.Count();

Console.WriteLine("Number of Students: {0}", numOfStudents);

Example: Count() in VB.Net

Dim studentList = New List(Of Student) From {
        New Student() With {.StudentID = 1, .StudentName = "John", .Age = 13},
        New Student() With {.StudentID = 2, .StudentName = "Moin", .Age = 21},
        New Student() With {.StudentID = 3, .StudentName = "Bill", .Age = 18},
        New Student() With {.StudentID = 4, .StudentName = "Ram", .Age = 20},
        New Student() With {.StudentID = 5, .StudentName = "Ron", .Age = 15}
}

Dim numOfStudents = studentList.Count()

Console.WriteLine("Number of Students: {0}", numOfStudents);

Output:
Number of Students: 5

In the following example, we get the count of students whose age is 18 or more by specifying condition in the Count method:

C#:

// Student collection
IList<Student> studentList = new List<Student>>() { 
        new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
        new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
        new Student() { StudentID = 5, StudentName = "Mathew" , Age = 15 } 
    };

var numOfStudents = studentList.Count(s => s.Age >= 18);

Console.WriteLine("Number of Students: {0}", numOfStudents);

Output:
Number of Students: 3
Note : Count() extension method with predicate parameter is Not Supported in VB.Net.

Count operator in query syntax:

Example: Count operator in query syntax VB.Net

// Student collection
Dim studentList = New List(Of Student) From {
        New Student() With {.StudentID = 1, .StudentName = "John", .Age = 13},
        New Student() With {.StudentID = 2, .StudentName = "Moin", .Age = 21},
        New Student() With {.StudentID = 3, .StudentName = "Bill", .Age = 18},
        New Student() With {.StudentID = 4, .StudentName = "Ram", .Age = 20},
        New Student() With {.StudentID = 5, .StudentName = "Ron", .Age = 15}
}

Dim numOfStudents = Aggregate st In studentList 
                    Into Count(st.Age >= 18)
             

Console.WriteLine("Number of Student: {0}", numOfStudents);

Output:
Number of Students: 3

C# Query Syntax doesn't support aggregation operators. However, you can wrap the query into brackets and use an aggregation functions as shown below.

Example: Count operator in query syntax C#

var totalAge = (from s in studentList
                select s.age).Count(); 

Learn about another aggregate operator - Max in the next section.