C# 8.0 introduced a new predefined structure that is known as Range struct. This struct is used to represent a range that has a start and end indexes. It provides a new style to create a range using .. operator. This operator is used to create a range that has a starting and ending index. Also with the help of the Range struct you are allowed to create a variable of the range type.
C#
// C# program to illustrate how to create ranges
usingSystem;
namespacerange_example {
classGFG {
// Main Method
staticvoidMain(string[] args)
{
int[] marks = newint[] {23, 45, 67, 88, 99,
56, 27, 67, 89, 90, 39};
// Creating variables of range type
// And initialize the range
// variables with a range
// Using .. operator
Range r1 = 1..5;
Range r2 = 6..8;
vara1 = marks[r1];
Console.Write("Marks List 1: ");
foreach(varst_1 ina1)
Console.Write($" {st_1} ");
vara2 = marks[r2];
Console.Write("\nMarks List 2: ");
foreach(varst_2 ina2)
Console.Write($" {st_2} ");
// Creating a range
// Using .. operator
vara3 = marks[2..4];
Console.Write("\nMarks List 3: ");
foreach(varst_3 ina3)
Console.Write($" {st_3} ");
vara4 = marks[4..7];
Console.Write("\nMarks List 4: ");
foreach(varst_4 ina4)
Console.Write($" {st_4} ");
}
}
}
Output:
Marks List 1: 45 67 88 99
Marks List 2: 27 67
Marks List 3: 67 88
Marks List 4: 99 56 27
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Please Login to comment...