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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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}, ");
    }
}
}

chevron_right


Output:

Range: 2..5
Numbers:  67,  78,  89, 

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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} ");
    }
}
}

chevron_right


Output:

Range: 0..3
Sports Name:  Archery  Badminton  Cricket 

Range: 4..7
Sports Name:  Boxing  Curling  Tennis 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.