I have a class that imlements IEquatable<T>
. Is it necessary to do a refrence check in Equals()
or is it taken care of in the framework?
class Foo : IEquatable<Foo>
{
int value;
Bar system;
bool Equals(Foo other)
{
return this == other ||
( value == other.value && system.Equals(other.system) );
}
}
In the above example is the this==other
statement superfluous or necessary?
Update 1
I understand I need to correct the code as follows:
bool Equals(Foo other)
{
if( other==null ) { return false; }
if( object.ReferenceEquals(this, other) ) { return true; } //avoid recursion
return value == other.value && system.Equals(other.system);
}
Thanks for the replies.