File tree Expand file tree Collapse file tree 4 files changed +104
-0
lines changed
Platform.Collections.Tests Expand file tree Collapse file tree 4 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -8,6 +8,42 @@ namespace Platform.Collections.Arrays
8
8
{
9
9
public static class GenericArrayExtensions
10
10
{
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
+
11
47
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
12
48
public static T [ ] Clone < T > ( this T [ ] array )
13
49
{
Original file line number Diff line number Diff line change @@ -8,6 +8,24 @@ namespace Platform.Collections.Lists
8
8
{
9
9
public static class IListExtensions
10
10
{
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
+
11
29
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
12
30
public static bool AddAndReturnTrue < T > ( this IList < T > list , T element )
13
31
{
You can’t perform that action at this time.
0 commit comments