Skip to content

Commit 23f94fd

Browse files
committed
Closes #63.
1 parent 8491785 commit 23f94fd

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Xunit;
2+
using Platform.Collections.Arrays;
3+
4+
namespace Platform.Collections.Tests
5+
{
6+
public class ArrayTests
7+
{
8+
[Fact]
9+
public void GetElementTest()
10+
{
11+
var nullArray = (int[])null;
12+
Assert.Equal(0, nullArray.GetElementOrDefault(1));
13+
Assert.False(nullArray.TryGetElement(1, out int element));
14+
Assert.Equal(0, element);
15+
var array = new int[] { 1, 2, 3 };
16+
Assert.Equal(3, array.GetElementOrDefault(2));
17+
Assert.True(array.TryGetElement(2, out element));
18+
Assert.Equal(3, element);
19+
Assert.Equal(0, array.GetElementOrDefault(10));
20+
Assert.False(array.TryGetElement(10, out element));
21+
Assert.Equal(0, element);
22+
}
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
using Xunit;
3+
using Platform.Collections.Lists;
4+
5+
6+
namespace Platform.Collections.Tests
7+
{
8+
public class ListTests
9+
{
10+
[Fact]
11+
public void GetElementTest()
12+
{
13+
var nullList = (IList<int>)null;
14+
Assert.Equal(0, nullList.GetElementOrDefault(1));
15+
Assert.False(nullList.TryGetElement(1, out int element));
16+
Assert.Equal(0, element);
17+
var list = new List<int>() { 1, 2, 3 };
18+
Assert.Equal(3, list.GetElementOrDefault(2));
19+
Assert.True(list.TryGetElement(2, out element));
20+
Assert.Equal(3, element);
21+
Assert.Equal(0, list.GetElementOrDefault(10));
22+
Assert.False(list.TryGetElement(10, out element));
23+
Assert.Equal(0, element);
24+
}
25+
}
26+
}

csharp/Platform.Collections/Arrays/GenericArrayExtensions.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,42 @@ namespace Platform.Collections.Arrays
88
{
99
public static class GenericArrayExtensions
1010
{
11+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12+
public static T GetElementOrDefault<T>(this T[] array, int index) => array != null && array.Length > index ? array[index] : default;
13+
14+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
15+
public static T GetElementOrDefault<T>(this T[] array, long index) => array != null && array.LongLength > index ? array[index] : default;
16+
17+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18+
public static bool TryGetElement<T>(this T[] array, int index, out T element)
19+
{
20+
if (array != null && array.Length > index)
21+
{
22+
element = array[index];
23+
return true;
24+
}
25+
else
26+
{
27+
element = default;
28+
return false;
29+
}
30+
}
31+
32+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
33+
public static bool TryGetElement<T>(this T[] array, long index, out T element)
34+
{
35+
if(array != null && array.LongLength > index)
36+
{
37+
element = array[index];
38+
return true;
39+
}
40+
else
41+
{
42+
element = default;
43+
return false;
44+
}
45+
}
46+
1147
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1248
public static T[] Clone<T>(this T[] array)
1349
{

csharp/Platform.Collections/Lists/IListExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@ namespace Platform.Collections.Lists
88
{
99
public static class IListExtensions
1010
{
11+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12+
public static T GetElementOrDefault<T>(this IList<T> list, int index) => list != null && list.Count > index ? list[index] : default;
13+
14+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
15+
public static bool TryGetElement<T>(this IList<T> list, int index, out T element)
16+
{
17+
if (list != null && list.Count > index)
18+
{
19+
element = list[index];
20+
return true;
21+
}
22+
else
23+
{
24+
element = default;
25+
return false;
26+
}
27+
}
28+
1129
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1230
public static bool AddAndReturnTrue<T>(this IList<T> list, T element)
1331
{

0 commit comments

Comments
 (0)