Finding all the Elements of a Range from Start to End 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 find all the range object starting from the start index to end with the help of All Property provided by the Range struct. This property always returns 0..^0 range.

Syntax:

public static property Range All { Range get(); };

Here, Range represents the index from start to end.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use of the 
// All property of the Range struct
using System;
  
namespace range_example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // Creating range
        // using Range Constructor
        var r = new Range(0, 5);
  
        // Getting the range objects from 
        // the starting index to end
        // Using All property
        var new_r = Range.All;
        Console.WriteLine(new_r);
    }
}
}

chevron_right


Output:

0..^0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to use 
// All property of the Range struct
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};
   
        // Finding all the range
        // Using All Property 
        // of Range struct
        var value = Range.All;
        var a = arr[value];
   
        // Displaying range and elements
        Console.WriteLine("Range: " + value);
        Console.WriteLine("Numbers: ");
   
        foreach(var i in a)
            Console.Write($"{i}, ");
    }
}
}

chevron_right


Output:

Range: 0..^0
Numbers:
23,  45,  67,  78,  89,  34,  89,  43,  67,  89, 


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.