简体   繁体   中英

Question regarding declaration of equality operators in C#

This seems incredibly basic, but I couldn't find any other answers on this particular note. In declaring a == operator in C#, you must also declare the != operator. Obviously every case may vary based on type, but if a type has explicit equality or does not, is it reasonable to declare != as simply !(a == b) ? Is there a reason NOT to do this? For example:

    public static bool operator ==(Point p1, Point p2)
    {
        return ((p1.X == p2.x) && (p1.Y == p2.Y));
    }

    public static bool operator !=(Point p1, Point p2)
    {
        return !(p1 == p2);
    }

There is a good example from Microsoft Docs: How to: Define Value Equality for a Type covering important aspects of defining equality for types.

In the following example, for x!=y you see it's simply returning !(x==y) :

using System;
class TwoDPoint : IEquatable<TwoDPoint>
{
    // Readonly auto-implemented properties.
    public int X { get; private set; }
    public int Y { get; private set; }

    // Set the properties in the constructor.
    public TwoDPoint(int x, int y)
    {
        if ((x < 1) || (x > 2000) || (y < 1) || (y > 2000))
        {
            throw new System.ArgumentException("Point must be in range 1 - 2000");
        }
        this.X = x;
        this.Y = y;
    }

    public override bool Equals(object obj)
    {
        return this.Equals(obj as TwoDPoint);
    }

    public bool Equals(TwoDPoint p)
    {
        // If parameter is null, return false.
        if (Object.ReferenceEquals(p, null))
        {
            return false;
        }

        // Optimization for a common success case.
        if (Object.ReferenceEquals(this, p))
        {
            return true;
        }

        // If run-time types are not exactly the same, return false.
        if (this.GetType() != p.GetType())
        {
            return false;
        }

        // Return true if the fields match.
        // Note that the base class is not invoked because it is
        // System.Object, which defines Equals as reference equality.
        return (X == p.X) && (Y == p.Y);
    }

    public override int GetHashCode()
    {
        return X * 0x00010000 + Y;
    }

    public static bool operator ==(TwoDPoint lhs, TwoDPoint rhs)
    {
        // Check for null on left side.
        if (Object.ReferenceEquals(lhs, null))
        {
            if (Object.ReferenceEquals(rhs, null))
            {
                // null == null = true.
                return true;
            }

            // Only the left side is null.
            return false;
        }
        // Equals handles case of null on right side.
        return lhs.Equals(rhs);
    }

    public static bool operator !=(TwoDPoint lhs, TwoDPoint rhs)
    {
        return !(lhs == rhs);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM