Skip to content

Instantly share code, notes, and snippets.

@vertigra
Last active March 5, 2017 12:37
Show Gist options
  • Select an option

  • Save vertigra/10dbdfb275d076f7e45a10d55336ea71 to your computer and use it in GitHub Desktop.

Select an option

Save vertigra/10dbdfb275d076f7e45a10d55336ea71 to your computer and use it in GitHub Desktop.
Проверка массива на null и 0 (C#)

Проверка массива на null и 0 (C#)

Отсюда

public static bool IsNullOrEmpty<T>(this IEnumerable<T> items)
{
  return items == null || !items.Any();
}

Checking For Empty Enumerations

Отсюда

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