This post will discuss how to find the size of a List or Set in C#.

1. Using Count Property

The standard solution to find the total number of elements in a List or Set is using its Count property. It returns the number of elements that are contained in the collection in O(1) time. The following example demonstrates its usage for a HashSet:

Download  Run Code

2. Using Count() Method

Alternatively, you can invoke LINQ’s Count() method on a collection to determine its size. The following code example demonstrates the usage of Count() method to count the elements in a HashSet.

Download  Run Code

That’s all about finding the size of a List or Set in C#.