The Range Structure is introduced in C# 8.0. It represents a range that has a start and end indexes. You are allowed to create a Range object starting from the specified starting index to the end of the given collection or sequence with the help of the StartAt() Method provided by the Range structure. Or in other words, StartAt() Method returns a range that starts from the specified start index to the end of the given collection or sequence.
Syntax:
public static Range StartAt(Index start);
Here, the Index start represents the start index.
Example 1:
using System;
namespace range_example {
class GFG {
static void Main( string [] args)
{
var r1 = new Range(2, 4);
Range r2 = 1..10;
var r3 = Range.StartAt(4);
Console.WriteLine( "Range_1: " + r1);
Console.WriteLine( "Range_2: " + r2);
Console.WriteLine( "Range_3: " + r3);
}
}
}
|
Output:
Range_1: 2..4
Range_2: 1..10
Range_3: 4..^0
Example 2:
using System;
namespace range_example {
class GFG {
static void Main( string [] args)
{
int [] arr = new int [8] {100, 200, 300,
400, 500, 600, 700, 800};
var r = Range.StartAt(2);
var new_arr = arr[r];
Console.WriteLine( "Range: " + r);
Console.Write( "Numbers: " );
foreach ( var i in new_arr)
Console.Write($ " [{i}]" );
}
}
}
|
Output:
Range: 2..^0
Numbers: [300] [400] [500] [600] [700] [800]