Mike-Ward.Net

IEnumerable Performance Tip: Any() vs. Count()

I can’t count the number of times (unintentional pun) I’ve checked if an IEnumerable<T> sequence contains elements using Count().

static void Method(IEnumerable<Status> statuses)  
{  
    if (statuses != null && statuses.Count() > 0)  
        // do something...  
}  

To get the count, the code has to traverse the entire sequence. On a long, lazy-executed sequences, this can take significant time. Since I only want to know if the sequence contains one or more elements, it’s computationally more efficient to use the Any() extension method.

static void Method(IEnumerable<Status> statuses)  
{  
    if (statuses != null && statuses.Any())  
        // do something...  
}  

In this case, Any() will return after examining the first element in the sequence. It also reads a better (IMHO).

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

VSColorOutput for VS 2017 Released
Quick ReSharper tip for inline templates
NoSQL vs. NewSQL: What's the Difference?
Exploring Mithril, A JavaScript Framework for Building Brilliant Applications
Tweetz 1.1
Tweetz Desktop 1.0
Tweetz 0.11 Released
More... (1073)

Donate with PayPal