public static bool IsNullOrEmpty<T>(this IEnumerable<T> items)
{
return items == null || !items.Any();
}
Last active
March 5, 2017 12:37
-
-
Save vertigra/10dbdfb275d076f7e45a10d55336ea71 to your computer and use it in GitHub Desktop.
Проверка массива на null и 0 (C#)
This solution, unfortunately, suffers from a common problem related to handling IEnumerables. The assumption that you can iterate over enumerable more than once.
A better solution is:
public static bool IsNullOrEmpty<T>(this IEnumerable<T> items, out IEnumerable<T> newItems)
{
newItems = items;
if(items == null)
return false;
var enumerator = items.GetEnumerator();
if(enumerator.MoveNext() == false)
return false;
newItems = new[]{enumerator.Current}.Concat(enumerator);
return true;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment