I recently saw a bit of code in a codebase I work with, where ReSharper offered to refactor it to collection.Any(Func< bool >)
.
I'm wondering about the performance impacts of this. Say I have a call that looks like this:
bool hasEvenValue = collection.Any(i => (i % 2) == 0);
...And data that looks like this...
{ 1, 2, 3, 5, 3, 5, 1, 3, 5, 2 }
When does Enumerable.Any() return the value? The second data element, or will it process every single element before returning true, in this instance?
Always and immediately when it's executed since it is not deferred executed. It returns a boolean which indicates whether or not one of the elements in the sequence returns true for the given predicate. Hence it doesn't need to execute the whole query unlike
Count
.Here's some code I whipped up in LINQPad to illustrate that the
Any
operator terminates after it hits the first match.Which outputs:
If the first
Any
call that tests "if any are even" walked the entire enumerable, the program would have terminated without displaying any messages. The second call was put in place to illustrate that the "if any are odd" test walks the entire list resulting in the exception being thrown.It returns as soon as it sees a matching element, or if none it processes the whole sequence.
For that reason it is better than using
.Count(...) != 0
(also more readable and semantically meaningful).Here's the implementation of
IEnumerable<T>.Any(...)
(uncompiled with dotKeep):So basically it returns as soon as an item satisfies the condition.