Starting from C# 8.0/.Net Core 3.0
Array slicing will be supported, along with the new types Index and Range being added.
Range Struct docs
Index Struct docs
Index i1 = 3; // number 3 from beginning
Index i2 = ^4; // number 4 from end
int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine($"{a[i1]}, {a[i2]}"); // "3, 6"
var slice = a[i1..i2]; // { 3, 4, 5 }
Above code sample taken from the C# 8.0 blog.
note that the ^ prefix indicates counting from the end of the array. As shown in the docs example
var words = new string[]
{
// index from start index from end
"The", // 0 ^9
"quick", // 1 ^8
"brown", // 2 ^7
"fox", // 3 ^6
"jumped", // 4 ^5
"over", // 5 ^4
"the", // 6 ^3
"lazy", // 7 ^2
"dog" // 8 ^1
}; // 9 (or words.Length) ^0
Range and Index also work outside of slicing arrays, for example with loops
Range range = 1..4;
foreach (var name in names[range])
Will loop through the entries 1 through 4
note that at the time of writing this answer, C# 8.0 is not yet officially released
C# 8.x and .Net Core 3.x are now available in Visual Studio 2019 and onwards