Skip to content

Commit

Permalink
added IsEmpty method for IEnumerable. Closes #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
jittuu committed Oct 18, 2011
1 parent 170a91f commit c7b2a3a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions NSupport.Test/EnumerableAccessTest.cs
Expand Up @@ -76,5 +76,26 @@ public class EnumerableAccessTest {
Assert.Equal(1, array.Count());
Assert.Single(array, "a");
}

[Fact]
public void Test_IsEmpty_with_null_collection() {
int[] nullArray = null;

Assert.Equal(true, nullArray.IsEmpty());
}

[Fact]
public void Test_IsEmpty_with_empty_collection() {
var emptyCol = Enumerable.Empty<int>();

Assert.Equal(true, emptyCol.IsEmpty());
}

[Fact]
public void Test_IsEmpty_with_non_empty_collection() {
var col = new int[] { 1 };

Assert.Equal(false, col.IsEmpty());
}
}
}
10 changes: 10 additions & 0 deletions NSupport/EnumerableAccess.cs
Expand Up @@ -27,5 +27,15 @@ public static class EnumerableAccess {
public static IEnumerable<T> To<T>(this IEnumerable<T> source, int index) {
return source.Take(++index);
}

/// <summary>
/// Determines whether a sequence is empty.
/// </summary>
/// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> to check for emptiness.</param>
/// <returns>true if the source sequence is null or contains no elements; otherwise, false.</returns>
public static bool IsEmpty<T>(this IEnumerable<T> source) {
return source == null || !source.Any();
}
}
}

0 comments on commit c7b2a3a

Please sign in to comment.