HasItems

/// <summary>
/// Determines whether an IEnumerable contains any item
/// </summary>
/// <param name="enumerable">the IEnumerable</param>
/// <returns>false if enumerable is null or contains no items</returns>
public static Boolean HasItems(this IEnumerable enumerable)
{
    if (enumerable == null)
        return false;

    try
    {
        var enumerator = enumerable.GetEnumerator();
        if (enumerator != null && enumerator.MoveNext())
        {
            return true;
        }
    }
    catch
    {

    }
    return false;
}
Example:
var strings = new []{"hello"};
strings.HasItems(); //true

strings = new String[0];
strings.HasItems(); //false

strings = null;
strings.HasItems(); //false

Description

Determines whether an IEnumerable contains any items

Details

Double click on the code to select all.

 

;