As the title says: do I need to override the ==
operator? how about the .Equals()
method? Anything I'm missing?
In a class, if you overload the Equals method, you should overload the == and != operators, but it is not required.
Because Complex is a value type, it cannot be derived from. Therefore, the override to Equals(Object) method need not call GetType to determine the precise run-time type of each object, but can instead use the is operator in C# or the TypeOf operator in Visual Basic to check the type of the obj parameter.
An example from msdn
public struct Complex { double re, im; public override bool Equals(Object obj) { return obj is Complex c && this == c; } public override int GetHashCode() { return re.GetHashCode() ^ im.GetHashCode(); } public static bool operator ==(Complex x, Complex y) { return x.re == y.re && x.im == y.im; } public static bool operator !=(Complex x, Complex y) { return !(x == y); } }
You should also implement IEquatable<T>. Here is an excerpt from Framework Design Guidelines:
DO implement IEquatable on value types. The Object.Equals method on value types causes boxing, and its default implementation is not very effcient because it uses refection. IEquatable.Equals can offer much better performance and can be implemented so that it does not cause boxing.
public struct Int32 : IEquatable<Int32> { public bool Equals(Int32 other){ ... } }
DO follow the same guidelines as for overriding Object.Equals when implementing IEquatable.Equals. See section 8.7.1 for detailed guidelines on overriding Object.Equals
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With