| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="Array"/>. |
| | 5 | | /// </summary> |
| | 6 | | public static class ArrayExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Gets the next item in the specified <see cref="Array"/>. |
| | 10 | | /// </summary> |
| | 11 | | /// <typeparam name="T">The array <see cref="Type"/>.</typeparam> |
| | 12 | | /// <param name="array">The array.</param> |
| | 13 | | /// <param name="item">The item.</param> |
| | 14 | | public static T? Next<T>(this Array? array, T? item) |
| 0 | 15 | | { |
| 0 | 16 | | if (array == null) return default; |
| | 17 | |
|
| 0 | 18 | | var indexOfKey = Array.IndexOf(array, item); |
| | 19 | |
|
| 0 | 20 | | ++indexOfKey; |
| | 21 | |
|
| 0 | 22 | | if (indexOfKey >= array.Length) return default(T); |
| | 23 | |
|
| 0 | 24 | | return (T) array.GetValue(indexOfKey)!; |
| 0 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Gets the previous item in the specified <see cref="Array"/>. |
| | 29 | | /// </summary> |
| | 30 | | /// <typeparam name="T">The array <see cref="Type"/>.</typeparam> |
| | 31 | | /// <param name="array">The array.</param> |
| | 32 | | /// <param name="item">The item.</param> |
| | 33 | | public static T? Previous<T>(this Array? array, T? item) |
| 0 | 34 | | { |
| 0 | 35 | | if (array == null) return default; |
| | 36 | |
|
| 0 | 37 | | var indexOfKey = Array.IndexOf(array, item); |
| | 38 | |
|
| 0 | 39 | | --indexOfKey; |
| | 40 | |
|
| 0 | 41 | | if (indexOfKey < 0) return default(T); |
| | 42 | |
|
| 0 | 43 | | return (T) array.GetValue(indexOfKey)!; |
| 0 | 44 | | } |
| | 45 | | } |