Get a subarray of an array between specified indices in C#
This post will discuss how to get a subarray of an array between specified indices in C#.
1. Using Array.Copy() method
A simple solution is to create a new array of required length and then call the Array.Copy() method to copy the required range of elements from the given array to the new array.
|
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 |
using System; public static class Extensions { public static T[] SubArray<T>(this T[] array, int offset, int length) { T[] result = new T[length]; Array.Copy(array, offset, result, 0, length); return result; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int offset = 4; int length = 3; array = array.SubArray(offset, length); Console.WriteLine(String.Join(",", array)); } } /* Output: 5,6,7 */ |
2. Using Enumerable.Skip with Enumerable.Take
The System.Linq.Enumerable.Skip() method bypasses the specified number of items in a sequence. It then returns the remaining elements, and the System.Linq.Enumerable.Take() method returns a specified number of contiguous elements from the start of a sequence.
The following code example demonstrates how the combination of Skip and Take can get subarray from the array.
|
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 |
using System; using System.Linq; public static class Extensions { public static T[] SubArray<T>(this T[] array, int offset, int length) { return array.Skip(offset) .Take(length) .ToArray(); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int offset = 4; int length = 3; array = array.SubArray(offset, length); Console.WriteLine(String.Join(",", array)); } } /* Output: 5,6,7 */ |
3. Using ArraySegment<T> Struct
To get a subarray, we can initialize a new instance of the ArraySegment<T> structure that delimits the specified range of the elements in the specified array.
|
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 |
using System; public static class Extensions { public static T[] SubArray<T>(this T[] array, int offset, int length) { return new ArraySegment<T>(array, offset, length) .ToArray(); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int offset = 4; int length = 3; array = array.SubArray(offset, length); Console.WriteLine(String.Join(",", array)); } } /* Output: 5,6,7 */ |
4. Using List.GetRange() method
Here, the idea is to convert the array into a list and then use its GetRange() method to get elements between the desired range. Then, we can use the ToArray() method to convert the list into an array.
The following example demonstrates how we can use the GetRange() method to get a slice of an array.
|
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 |
using System; using System.Collections.Generic; public static class Extensions { public static T[] SubArray<T>(this T[] array, int offset, int length) { return new List<T>(array) .GetRange(offset, length) .ToArray(); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int offset = 4; int length = 3; array = array.SubArray(offset, length); Console.WriteLine(String.Join(",", array)); } } /* Output: 5,6,7 */ |
That’s all about getting a subarray of an array between specified indices in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)