Use .Any instead of .Count

I am amazed by how many developers use IEnumerable<T>.Count() when they should use IEnumerable<T>.Any().

How many times have you seen/written this:

if (collection.Count() > 0)
    // Now that we know we have some elements,
    // let's do something meaningful

There are (at least) 3 basic flaws with this approach:

1. Highly inefficient
2. Semantical implications
3. Infinite loops

Continue reading