Skip to content

Commit

Permalink
added Empty method to T[].
Browse files Browse the repository at this point in the history
  • Loading branch information
jittuu committed Nov 6, 2012
1 parent 2f20f04 commit f7d5e19
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions NSupport.Test/ArrayAccessTest.cs
Expand Up @@ -38,5 +38,21 @@ public class ArrayAccessTest {
var ints = new[] { 1, 2, 3, 4, 5, 1 };
Assert.Equal(-1, ints.LastIndexOf(7));
}

[Fact]
public void Test_Empty_with_null() {
int[] ints = null;
Assert.Throws<ArgumentNullException>(() => ints.Empty());
}

[Fact]
public void Test_Empty_with_existing_value() {
var ints = new[] { 1, 2, 3, 4, 5, 1 };

var values = ints.Empty();

Assert.NotEmpty(ints);
Assert.Empty(values);
}
}
}
12 changes: 12 additions & 0 deletions NSupport/ArrayAccess.cs
Expand Up @@ -30,5 +30,17 @@ public static class ArrayAccess {

return Array.LastIndexOf<T>(source, value);
}

/// <summary>
/// Create empty array of T without modifying <paramref name="source"/>.
/// </summary>
/// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An instance of <see cref="Array"/>.</param>
/// <returns>Empty array of T.</returns>
public static T[] Empty<T>(this T[] source) {
Guard.ArgumentNotNull("source", source);
source = new T[] { };
return source;
}
}
}

0 comments on commit f7d5e19

Please sign in to comment.