What is the “best” canonical implementation of Equals() for reference types?
Question
Implementing Equals() for reference types is harder than it seems. My current canonical implementation goes like this:
public bool Equals( MyClass obj )
{
// If both refer to the same reference they are equal.
if( ReferenceEquals( obj, this ) )
return true;
// If the other object is null they are not equal because in C# this cannot be null.
if( ReferenceEquals( obj, null ) )
return false;
// Compare data to evaluate equality
return _data.Equals( obj._data );
}
public override bool Equals( object obj )
{
// If both refer to the same reference they are equal.
if( ReferenceEquals( obj, this ) )
return true;
// If the other object is null or is of a different types the objects are not equal.
if( ReferenceEquals( obj, null ) || obj.GetType() != GetType() )
return false;
// Use type-safe equality comparison
return Equals( (MyClass)obj );
}
public override int GetHashCode()
{
// Use data's hash code as our hashcode
return _data.GetHashCode();
}
I think that this covers all corner (inheritance and such) cases but I may be wrong. What do you guys think?
Solution
I wrote a fairly comprehensive guide to this a while back. For a start your equals implementations should be shared (i.e. the overload taking an object should pass through to the one taking a strongly typed object). Additionally you need to consider things such as your object should be immutable because of the need to override GetHashCode. More info here:
http://gregbeech.com/blog/implementing-object-equality-in-dotnet
OTHER TIPS
Better hope that this._data is not null if it's also a reference type.
public bool Equals( MyClass obj )
{
if (obj == null) {
return false;
}
else {
return (this._data != null && this._data.Equals( obj._data ))
|| obj._data == null;
}
}
public override bool Equals( object obj )
{
if (obj == null || !(obj is MyClass)) {
return false;
}
else {
return this.Equals( (MyClass)obj );
}
}
public override int GetHashCode() {
return this._data == null ? 0 : this._data.GetHashCode();
}
Concerning inheritance, I think you should just let the OO paradigm does its magic.
Specifically, the GetType()
check should be removed, it might break polymorphism down the line.
I agree with chakrit, objects of different types should be allowed to be semantically equal if they have the same data or ID.
Personally, I use the following:
public override bool Equals(object obj)
{
var other = obj as MyClass;
if (other == null) return false;
return this.data.Equals(other.data);
}
It depends on whether you're writing a value type or a reference type. For a sortable value type, I recommend this: A code snippet for Visual Studio 2005 that implements a skeleton value type adhering to Framework Design Guidelines