To get a sub-array in C#, you can use the `ArraySegment` struct or array slicing.
In this example,
numbers
with some values.using System;
class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };
int[] subArray = new ArraySegment<int>(numbers, 2, 2).ToArray();
Console.WriteLine("Sub-array: " + string.Join(", ", subArray));
}
}
Sub-array: 3, 4
In this example,
colors
with some values.using System;
class Program
{
static void Main()
{
string[] colors = { "red", "green", "blue", "yellow", "orange" };
string[] subArray = new ArraySegment<string>(colors, 1, 2).ToArray();
Console.WriteLine("Sub-array: " + string.Join(", ", subArray));
}
}
Sub-array: green, blue
In this tutorial, we learned How to get Sub Array in C# language with well detailed examples.