Permalink
Browse files

added IndexOf and LastIndexOf method to array as extension method.

  • Loading branch information...
1 parent ab1cbac commit 3938777a983b84a9130d2d2dc8148ae2fbc1fc88 @jittuu committed Jan 19, 2012
Showing with 68 additions and 0 deletions.
  1. +36 −0 NSupport.Test/ArrayAccessTest.cs
  2. +1 −0 NSupport.Test/NSupport.Test.csproj
  3. +30 −0 NSupport/ArrayAccess.cs
  4. +1 −0 NSupport/NSupport.csproj
@@ -0,0 +1,36 @@
+namespace NSupport.Test {
+ using Xunit;
+ using System;
+
+ public class ArrayAccessTest {
+ [Fact]
+ public void Test_IndexOf_with_null() {
+ int[] nullArray = null;
+ Assert.Throws<ArgumentNullException>(() => nullArray.IndexOf(1));
+ }
+
+ [Fact]
+ public void Test_IndexOf_with_existing_value() {
+ var ints = new[] { 1, 2, 3, 4, 5, 1 };
+ Assert.Equal(0, ints.IndexOf(1));
+ }
+
+ [Fact]
+ public void Test_IndexOf_with_non_existing_value() {
+ var ints = new[] { 1, 2, 3, 4, 5 };
+ Assert.Equal(-1, ints.IndexOf(7));
+ }
+
+ [Fact]
+ public void Test_LastIndexOf_with_existing_value() {
+ var ints = new[] { 1, 2, 3, 4, 5, 1 };
+ Assert.Equal(5, ints.LastIndexOf(1));
+ }
+
+ [Fact]
+ public void Test_LastIndexOf_with_non_existing_value() {
+ var ints = new[] { 1, 2, 3, 4, 5, 1 };
+ Assert.Equal(-1, ints.LastIndexOf(7));
+ }
+ }
+}
@@ -47,6 +47,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
+ <Compile Include="ArrayAccessTest.cs" />
<Compile Include="DateTimeCalculationsTest.cs" />
<Compile Include="DateTimeOffsetCalculationsTest.cs" />
<Compile Include="EnumerableAccessTest.cs" />
View
@@ -0,0 +1,30 @@
+namespace NSupport {
+ using System;
+
+ /// <summary>
+ /// Provide access methods for <see cref="Array"/>
+ /// </summary>
+ public static class ArrayAccess {
+ /// <summary>
+ /// The zero-based index of the first occurrence of value within the entire <paramref name="source"/>, if found; otherwise, –1.
+ /// </summary>
+ /// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
+ /// <param name="source">An instance of <see cref="Array"/>.</param>
+ /// <param name="value">The object to locate in <paramref name="source"/>.</param>
+ /// <returns>The zero-based index of the first occurrence of value within the entire <paramref name="source"/>, if found; otherwise, –1.</returns>
+ public static int IndexOf<T>(this T[] source, T value) {
+ return Array.IndexOf<T>(source, value);
+ }
+
+ /// <summary>
+ /// The zero-based index of the last occurrence of value within the entire <paramref name="source"/>, if found; otherwise, –1.
+ /// </summary>
+ /// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
+ /// <param name="source">An instance of <see cref="Array"/>.</param>
+ /// <param name="value">The object to locate in <paramref name="source"/>.</param>
+ /// <returns>The zero-based index of the last occurrence of value within the entire <paramref name="source"/>, if found; otherwise, –1.</returns>
+ public static int LastIndexOf<T>(this T[] source, T value) {
+ return Array.LastIndexOf<T>(source, value);
+ }
+ }
+}
View
@@ -43,6 +43,7 @@
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="ArrayAccess.cs" />
<Compile Include="DateTimeCalculations.cs" />
<Compile Include="DateTimeOffsetCalculations.cs" />
<Compile Include="EnumerableAccess.cs" />

0 comments on commit 3938777

Please sign in to comment.