Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Added EmptyIfNull. Closes #9.
  • Loading branch information
bgrainger committed Feb 7, 2010
1 parent c2cb263 commit c48cb52
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Logos.Utility/EnumerableUtility.cs
Expand Up @@ -10,6 +10,17 @@ namespace Logos.Utility
/// </summary>
public static class EnumerableUtility
{
/// <summary>
/// Returns the source sequence, or an empty sequence if <paramref name="source"/> is <c>null</c>.
/// </summary>
/// <param name="source">The source sequence.</param>
/// <returns>The source sequence, or an empty sequence if <paramref name="source"/> is <c>null</c>.</returns>
/// <remarks>See <a href="http://code.logos.com/blog/2008/03/emptyifnull.html">EmptyIfNull</a>.</remarks>
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source)
{
return source ?? Enumerable.Empty<T>();
}

/// <summary>
/// Computes the sum of a sequence of <see cref="Nullable{Decimal}"/> values.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions tests/Logos.Utility.Tests/EnumerableUtilityTests.cs
Expand Up @@ -9,6 +9,18 @@ namespace Logos.Utility.Tests
[TestFixture]
public class EnumerableUtilityTests
{
[Test]
public void EmptyIfNullNull()
{
CollectionAssert.AreEqual(new int[0], ((IEnumerable<int>) null).EmptyIfNull());
}

[Test]
public void EmptyIfNullNonNull()
{
CollectionAssert.AreEqual(new[] { 1, 2 }, (new[] { 1, 2 }).EmptyIfNull());
}

const int NullInt = 1234567;

[TestCase(new[] { 1, 2, 3, 4, 5 }, 15)]
Expand Down

0 comments on commit c48cb52

Please sign in to comment.