HasItems
Determines whether an IEnumerable contains any items
Source
/// <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
Author: Juan Agüí
Submitted on: 25 okt. 2011
Language: C#
Type: System.Collections.IEnumerable
Views: 6426