Skip to content

Files

Latest commit

 

History

History
37 lines (26 loc) · 844 Bytes

File metadata and controls

37 lines (26 loc) · 844 Bytes

Pattern: Use of Enumerable.Count()

Issue: -

Description

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;
}

Further Reading