Equal values ββ-
1- Override the GetHashCode method to allow the type to work correctly in the hash table.
2 Do not throw an exception in the implementation of the Equals method. Instead, return false for the null argument.
3 -
x.Equals(x) returns true. x.Equals(y) returns the same value as y.Equals(x). (x.Equals(y) && y.Equals(z)) returns true if and only if x.Equals(z) returns true.
Successive calls to x.Equals (y) return the same value if the object referenced by x and y does not change.
x.Equals(null) returns false.
4- For some types of objects, it is desirable to have an Equals test for equality of values ββinstead of reference equality. Such Equals implementations return true if both objects have the same value, even if they are not the same instance.
Example -
Object obj1 = new Object(); Object obj2 = new Object(); Console.WriteLine(obj1.Equals(obj2)); obj1 = obj2; Console.WriteLine(obj1.Equals(obj2));
Output: -
False True
and compareTo is
Compares the current instance with another object of the same type and returns an integer indicating whether the current instance precedes, follows or occurs at the same position in the sort order as the other object.
He returns -
Less than zero - this instance precedes obj in sort order. Zero - this instance is found in the same position in the sort order as obj. Greater than zero - this instance follows obj in sort order.
It can raise an ArgumentException if the object is not the same type as the instance.
For example, you can visit here .
Therefore, I suggest it is better to use Equals instead of compareTo.
source share