This post will discuss how to get the count of the number of items in a list in C#.

The Count property returns the number of elements contained in the List. This works in O(1) time and can be used as follows:

Download  Run Code

Output:

The number of elements in the list is 5

 
In LINQ, you can use the Count() extension method to obtain the count of elements, as the following example illustrates. Note that it might translate to an invocation of List’s Count property.

Download  Run Code

Output:

The number of elements in the list is 5

 
Alternatively, to get the count of a single element, filter the list using the Where() method to obtain matching values with the specified target, and then get its count by invoking the Count() method.

Download  Run Code

Output:

Element 2 occurs 3 times

That’s all about getting the count of the number of items in a list in C#.