Pattern: Use of Enumerable.Count()
Issue: -
This rule flags the Count LINQ method calls on collections of types that have equivalent, but more efficient Length
or Count
property to fetch the same data. Length
or Count
property does not enumerate the collection, hence is more efficient.
Example of incorrect code:
class Test
{
public int GetCount(int[] array)
=> array.Count();
public int GetCount(ICollection<int> collection)
=> collection.Count();
}
Example of correct code:
class Test
{
public int GetCount(int[] array)
=> array.Length;
public int GetCount(ICollection<int> collection)
=> collection.Count;
}