Alex Rivera | Logout

How to check if IEnumerable is null or empty?

Asked 2011-02-18T22:44:21.073
222

I love string.IsNullOrEmpty method. I'd love to have something that would allow the same functionality for IEnumerable. Is there such? Maybe some collection helper class? The reason I am asking is that in if statements the code looks cluttered if the patter is (mylist != null && mylist.Any()). It would be much cleaner to have Foo.IsAny(myList).

This post doesn't give that answer: IEnumerable is empty?.

Edit
Report

1 Answer

136
public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable) {
    return enumerable == null || !enumerable.Any();
}
answered 2011-02-18T22:47:00.273

Your Answer