enumerations

There are 1 entries for the tag enumerations

Checking For Empty Enumerations

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...