Open In App
Related Articles

Finding all the Elements of a Range from Start to End in C#

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:
 

CSharp

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

                    

Output: 
 

0..^0


Example 2:
 

CSharp

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

                    

Output: 
 

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


 



Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads