Do I need to check the Count() of an Enumerable before foreach

c#

Solution

No, the opposite can be true. If it's not a collection (like a `List` or `Array`) but a deferred executed query it must be executed completely which can be very expensive, just to determine the count. In your example it's actually a `List`, `Enumerable.Count` is clever enough to try to cast it to a `ICollection<T>`/`ICollection` first . If that succeeds the `Count` property is used.

So just use the `foreach`. It doesn't hurt if the sequence is empty, the loop will be exited immediately.

For the same reason it's better to use `Enumerable.Any` instead of `Count() > 0` if you just want to check if a sequence contains at least one element. The intention is also more clear.

Problem

Is there any speed improvement or indeed point in checking the `Count()` of an Enumerable before iterating/foreaching over the collection? ``` List<int> numbers = new List<int>(); if(numbers.Count() > 0) { foreach(var i in numbers) {/*Do something*/} } ```

Original source

Related problems