Overloading Equality Operators
As you may recall from Chapter 6, System.Object.Equals() can be overridden to perform value-
based (rather than referenced-based) comparisons between types. If you choose to override
Equals() (and the often related System.Object.GetHashCode() method), it is trivial to overload
the equality operators (== and !=). To illustrate, here is the updated Point type:
// This incarnation of Point also overloads the == and != operators.
public struct Point
{
...
public override bool Equals(object o)
{
return o.ToString() == this.ToString();
}
public override int GetHashCode()
{
return this.ToString().GetHashCode(); }
Do'stlaringiz bilan baham: |