Calculate the number of elements in your array, List, or ArrayList. Collections in C# generally store their count in a field, which you can get from a property. There is also the extension method Count().
Run tests on many different kinds of collections looking for the correct properties to access. When you get the count of a collection you use a property access (getter). This indicates that it won't take a long time to calculate.
The ArrayList is a non-generic collection that stores its length in a property called Count. This is identical to the List collection that was introduced in C# later. This property access is very fast.
class Program
{
static void Main(string[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.Add("One");
arrayList.Add("Two");
// Count the elements in the ArrayList.
int count1 = arrayList.Count;
// 2
}
}
List in C# is a generic collection, which means you must specify its type in the sharp angle brackets <T>. These collections have far superior performance and are almost always preferrable. List has a property called Count that is fast to access.
List<int> listTest = new List<int>(); listTest.Add(1); listTest.Add(2); // Count with the Count property. int count2 = listTest.Count; // 2 // Count with the extension method. This is different! int count3 = listTest.Count(); // BE CAREFUL! // 2
Arrays are counted differently and you need to use Length. This may be because array size is set differently than List or ArrayList, and it does not grow dynamically. In any case, arrays are measured with Length. Length will include all elements.
string[] stringArray = new string[]
{
"One",
"Two",
"Three"
};
int count4 = stringArray.Length;
// 3
long count5 = stringArray.LongLength; // Only use on huge collections.
// 3
int count6 = stringArray.Count(); // Extension method.
// 3
string[] emptyArray = new string[5];
int countA = emptyArray.Length;
// 5
| Property | Type | Its use in C# |
| Length | int |
The number of elements in an array. Is extremely fast to retrieve. |
| LongLength | long |
Specialty property that stores Length. |
| Count() | int |
An extension method. Iterates over each element. Slow. |
When you have an IEnumerable collection or are using LINQ statements. Enumerations in C# are collections that haven't been determined exactly yet, and they can use yield and foreach keywords. In LINQ, an enumerable collection doesn't really exist until you use it.
// An enumerable collection, not in memory yet. // (Read more about the var keyword.) var enumerableItem = from item in stringArray select item; int count7 = enumerableItem.Count(); // Extension method. // ... does a lot of work to get the count. // 3
A lot slower. In my benchmark, where I took the Count of an array 10,000 times, the Count() extension performed more than 10 times slower. It was likely iterating through the collection each time. The benchmark tested 10,000 iterations of 10,000 iterations.
Use the Count property on generic List collections or ArrayList, and the Length property on arrays. These properties are likely already computed and are very fast. Avoid the Count() extension method from LINQ in normal situations.