Check if the given ranges are equal or not 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 check the given ranges are equal or not with the help of following methods provided by the Range struct:

1. Equals(Object): This method is Object Class method which returns a value that shows the given range object is equal to the other range object. The result is in the form of bool if it returns true, then the current range object is equal to another range object or if it returns false, then the current range object is not equal to another range object.

Syntax:



public override bool Equals(System::Object ^ value);

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to 
// check the given ranges is equal or not
// Using Equals(object) method of Range struct
using System;
  
namespace range_example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Creating a range
        // using StartAt() method
        var r1 = Range.StartAt(3);
        var r2 = Range.StartAt(4);
  
        // Checking the ranges
        // are equal or not
        // Using Equals(Object) method
        if (r1.Equals(r2)) {
  
            Console.WriteLine("Both range 1{0} and range"+
                              " 2 {1} are equal", r1, r2);
        }
  
        else {
  
            Console.WriteLine("Both ranges are not equal");
        }
    }
}
}

chevron_right


Output:

Both ranges are not equal

2. Equals(Range): This method returns a value that shows the given object is equal to the other object. The result is in the form of bool if it returns true, then the current object is equal to another range object or if it returns false, then the current object is not equal to another range object.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to 
// check the given ranges is equal or not
// Using Equals(Range) method of Range struct
using System;
  
namespace range_example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Creating a ranges
        Range r1 = 1..8;
        Range r2 = 1..8;
  
        // Checking the ranges
        // are equal or not
        // Using Equals(Range) method
        if (r1.Equals(r2)) {
  
            Console.WriteLine("Both ranges are equal");
        }
  
        else {
  
            Console.WriteLine("Both ranges are not equal");
        }
    }
}
}

chevron_right


Output:

Both ranges are equal


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.