Possible Duplicate:
How do I check for nulls in an ‘==’ operator overload without infinite recursion?
Say I have a type like this:
public class Effect
{
public static bool operator == ( Effect a, Effect b )
{
return a.Equals ( b );
}
public static bool operator != ( Effect a, Effect b )
{
return !a.Equals ( b );
}
public bool Equals ( Effect effect )
{
return this.TypeID.Equals ( effect.TypeID );
}
public override bool Equals ( object obj )
{
return this.TypeID.Equals ( ( ( Effect ) obj ).TypeID );
}
}
What's the most robust and cleanest way to handle null values?
I am not sure if I have to check for null for both the current instance (this
) and the passed instance (effect/obj)
? If I have null for the current instance (this
), would the compiler still call effect.Equals or Object.Equals?
Also either way where should the null checks be done? I am assuming only inside the Equals methods, and not the equality operators (==, !=
).