我有一堂课可以实施IEquatable<T>
。是否有必要进行参考检查Equals()
还是在框架中进行了处理?
class Foo : IEquatable<Foo>
{
int value;
Bar system;
bool Equals(Foo other)
{
return this == other ||
( value == other.value && system.Equals(other.system) );
}
}
在上面的例子中,this==other
陈述是多余的还是必要的?
更新 1
我知道我需要更正代码如下:
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);
}
感谢您的回复。