-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArrayExtension.cs
More file actions
36 lines (30 loc) · 1.24 KB
/
ArrayExtension.cs
File metadata and controls
36 lines (30 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
namespace Substrate.NET.Wallet.Extensions
{
public static class ArrayExtension
{
/// <summary>
/// Read an array from start index to end index
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static T[] SubArray<T>(this T[] array, int start, int end)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
if (start < 0 || start >= array.Length)
throw new ArgumentOutOfRangeException(nameof(start), "Start index is out of bounds.");
if (end < start || end > array.Length)
throw new ArgumentOutOfRangeException(nameof(end), "End index is out of bounds or less than start index.");
int length = end - start;
T[] result = new T[length];
Array.Copy(array, start, result, 0, length);
return result;
}
public static T[] SubArray<T>(this T[] array, int start) => array.SubArray(start, array.Length);
}
}