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:
using System;
namespace range_example {
class GFG {
static void Main( string [] args)
{
var r1 = Range.StartAt(3);
var r2 = Range.StartAt(4);
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:
using System;
namespace range_example {
class GFG {
static void Main( string [] args)
{
Range r1 = 1..8;
Range r2 = 1..8;
if (r1.Equals(r2)) {
Console.WriteLine( "Both ranges are equal" );
}
else {
Console.WriteLine( "Both ranges are not equal" );
}
}
}
}
|
Output:
Both ranges are equal
Last Updated :
28 Nov, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...