While spelunking in some code recently I saw a method that looked something like this: public void Foo<T>(IEnumerable<T> items) { if (items == null || items.Count() == 0) { // Warn about emptiness } } This method accepts a generic enumeration and then proceeds to check if the enumeration is null or empty. Do you see the potential problem with this code? I'll give you a hint, it's this line: items.Count() == 0 What's the problem? Well that line right there has the potential to be vastly inefficient. If the caller of the Foo method passes in an enumeration that doesn't implement ICollection<T> ( for example, an IQueryable as a result from an Entity Framework or Linq to SQL query ) then the Count method has to iterate over the...(read more) ...
Go to the complete details ...