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:
// 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
Recommended Posts:
- Check if two enums are equal or not in C#
- Check if ValueTuple instances are equal in C#
- C# | Check if two ArrayList objects are equal
- Check if two SortedDictionary objects are equal in C#
- Check if two Dictionary objects are equal in C#
- C# | Check if two ListDictionary objects are equal
- C# | Check if two SortedList objects are equal
- C# | Check if two SortedSet<T> objects are equal
- C# | Check if two StringBuilder objects are Equal
- C# | Check if two HashSet<T> objects are equal
- C# | Check if two StringDictionary objects are equal or not
- C# | Check if two LinkedList<T> objects are equal
- C# | Check if two Tuple Objects are equal
- C# | Check if two BitArray objects are equal
- C# | Check if two List objects are equal
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.


