Skip to content

Files

Latest commit

 

History

History
31 lines (22 loc) · 753 Bytes

File metadata and controls

31 lines (22 loc) · 753 Bytes

Pattern: Use of Count instead of IsEmpty

Issue: -

Description

The Count or Length property or the Count<TSource>(IEnumerable<TSource>) extension method was used to determine whether or not the object contains any items by comparing the value to 0 or 1, and the object has a more efficient IsEmpty property that could be used instead.

Example of incorrect code:

class Test
{
    ConcurrentQueue<int> _queue;
    public bool HasNoItems => _queue.Count == 0;
}

Example of correct code:

class Test
{
    ConcurrentQueue<int> _queue;
    public bool HasNoItems => _queue.IsEmpty;
}

Further Reading