While learning .net (by c#) i found 5 ways for checking equality between objects.
- The ReferenceEquals() method.
- The virtual Equals() method. (System.Object)
- The static Equals() method.
- The Equals method from IEquatable interface.
- The comparison operator == .
My question is :
- Why there are so many Equals() method along with comparison operator?
- Which one of the virtual Equals() or IEquatable’s Equals() sholud be used .. (say if we use our own collection classes)
1 – Reference equals checks if two reference type variables(classes, not structs) are referred to the same memory adress.
2 – The virtual Equals() method checks if two objects are equivalent. Let us say that you have this class:
and you instantiate 2 objects from that class:
although the two objects are not the same instance of TestClass, the call to o1.Equals(o2) will return true.
3 – The static Equals method is used to handle problems when there is a null value in the check.
Imagine this, for instance:
If you try this:
you wil get a NullReferenceException, because o1 points to nothing.
To adress this issue, you do this:
Object.Equals(o1,o2);
This method is prepared to handle null references.
4 – The IEquatable interface is provided by .Net so you don’t need to do casts inside your Equals method.
If the compiler finds out that you have implemented the interface in a class for the type you are trying to check for equality, it will give that method priority over the Object.Equals(Object) override.
For instance:
now if we do this:
The called method is Equals(TestClass), prior to Equals(Object).
5 – The == operator usually means the same as ReferenceEquals, it checks if two variables point to the same memory adress.
The gotcha is that this operator can be overrided to perform other types of checks.
In strings, for instance, it checks if two different instances are equivalent.
This is a usefull link to understand equalities in .Net better: