How to Create a Range From a Specified Start in C#?
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:
// C# program to illustrate // how to create a range using // StartAt() method of Range struct using System; namespace range_example { class GFG { // Main Method static void Main(string[] args) { // Creating range // using Range constructor var r1 = new Range(2, 4); // Creating range // using Range operator Range r2 = 1..10; // Creating a range // using StartAt() method var r3 = Range.StartAt(4); // Displaying all the ranges 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:
// C# program to illustrate how // to create a range using // StartAt() method of Range struct using System; namespace range_example { class GFG { // Main Method static void Main(string[] args) { // Creating and initializing an array int[] arr = new int[8] {100, 200, 300, 400, 500, 600, 700, 800}; // Creating a range // using StartAt() method var r = Range.StartAt(2); var new_arr = arr[r]; // Displaying the range // and the elements 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]
Recommended Posts:
- Finding all the Elements of a Range from Start to End in C#
- Finding the Start Index of the Specified Range in C#
- How to Create a Range to a Specified End in C#?
- How to check whether the index is from start or end in C#?
- C# | Removing the node at the start of the LinkedList<T>
- C# | Adding new node or value at the start of LinkedList<T>
- Creating an Index From the Specified Index at the Start of a Collection in C#
- How to create a Queue in C#
- How to create 1-ValueTuple in C#?
- C# | How to create a SortedSet
- How to create a StringCollection in C#
- How to create an OrderedDictionary in C#
- How to create a StringDictionary in C#
- How to create the ArrayList in C#
- C# | How to create a Stack
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


