Range Constructor in C#
Range(Index, Index) Constructor is the part of Range Struct. This constructor is used to create a new instance of Range along with the specified starting and ending indexes. When you create a range using the range operator or constructor, then it will not add the last element or end index element.
For example, we have an array {1, 2, 3, 4, 5, 6 }, now we want to print range[1..3], then it will print 2, 3. It does not print 2, 3, 4.
Syntax:
public Range(Index start, Index end);
Here, the start represents the starting index of the range and the end represents the last index of the range.
Example 1:
// C# program to illustrate how to // use Range(Index, Index) constructor using System; namespace range_example { class GFG { // Main Method static void Main(string[] args) { // Creating and initailizing an array int[] arr = new int[10] {23, 45, 67, 78, 89, 34, 89, 43, 67, 89}; Index start = 2; Index end = 5; // Creating range // Using Range(Index, // Index) Constructor var r = new Range(start, end); var value = arr[r]; // Displaying range and elements Console.WriteLine("Range: " + r); Console.Write("Numbers: "); foreach(var i in value) Console.Write($" {i}, "); } } } |
Output:
Range: 2..5 Numbers: 67, 78, 89,
Example 2:
// C# program to illustrate how to // use Range(Index, Index) constructor using System; namespace range_example { class Program { // Main Method static void Main(string[] args) { // Creating and initailizing an array string[] arr = new string[8] {"Archery", "Badminton", "Cricket", "Bowling", "Boxing", "Curling", "Tennis", "Skateboarding"}; // Creating ranges // Using Range(Index, // Index) Constructor var r1 = new Range(0, 3); var r2 = new Range(4, 7); var value_1 = arr[r1]; var value_2 = arr[r2]; // Displaying range and elements Console.WriteLine("Range: " + r1); Console.Write("Sports Name: "); foreach(var i_1 in value_1) Console.Write($" {i_1} "); Console.WriteLine("\n\nRange: " + r2); Console.Write("Sports Name: "); foreach(var i_2 in value_2) Console.Write($" {i_2} "); } } } |
Output:
Range: 0..3 Sports Name: Archery Badminton Cricket Range: 4..7 Sports Name: Boxing Curling Tennis
Recommended Posts:
- C# | Copy Constructor
- Index Constructor in C#
- C# | Constructor Overloading
- C# | Default Constructor
- C# | Thread(ThreadStart) Constructor
- C# | Thread(ParameterizedThreadStart) Constructor
- Invoking an overloaded constructor using this keyword in C#
- Range Structure in C# 8.0
- How to Create a Range to a Specified End in C#?
- Range and Indices in C# 8.0
- How to Create a Range From a Specified Start in C#?
- Finding the End Index of the Specified Range in C#
- Getting the Hash Code of the Specified Range in C#
- Finding all the Elements of a Range from Start to End in C#
- C# | Removing a range of elements from the List
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.


