Skip to content
Related Articles

Related Articles

Check if the given ranges are equal or not in C#
  • Last Updated : 28 Nov, 2019
GeeksforGeeks - Summer Carnival Banner

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:




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

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:




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

Output:

Both ranges are equal

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :