Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator overloading giving error [duplicate]

Tags:

c#

.net

I have a class named Point that overloading "==" and "!=" operators to compare two Point object. How can I compare my Point object with "null", this a problem because when I call == or != operator with null a problem inside Equals method. Please open a console application and see what I want to say.How can I fix it.

public class Point
    {
        public int X { get; set; }
        public int Y { get; set; }


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

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


        public override bool Equals(object obj)
        {
            Point other = obj as Point;

            //problem is here calling != operator and this operator calling  this method again
            if (other != null)
            {
                if (this.X == other.X && this.Y == other.Y)
                {
                    return true;
                }
                return false;
            }

            else
            {
                throw new Exception("Parameter is not a point");
            }
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            Point p1 = new Point { X = 9, Y = 7 };
            Point p2 = new Point { X = 5, Y = 1 };

            p1.X = p2.X;
            p1.Y = p2.Y;

            bool b1=p1==p2;

            Console.ReadKey();
        }
    }
like image 741
erhan urun Avatar asked Dec 14 '25 06:12

erhan urun


1 Answers

Use ReferenceEquals to check for null:

if (ReferenceEquals(other, null))

Having said that, Equals should generally not throw an exception if it encounters an unknown object type, it should just return false, thus here is the method I would write:

public override bool Equals(object obj)
{
    Point other = obj as Point;

    if (ReferenceEquals(other, null))
        return false;

    return (this.X == other.X && this.Y == other.Y);
}

An additional problem is that your operators will throw an exception if you compare null against something, as you can't invoke the instance method on a null reference.

Thus, here is the full class I would've written:

public class Point
{
    public int X { get; set; }
    public int Y { get; set; }


    public static bool operator == (Point p1,Point p2)
    {
        if (ReferenceEquals(p1, p2)) return true;
        if (ReferenceEquals(p1, null)) return false;
        return p1.Equals(p2);
    }

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

    public bool Equals(Point other)
    {
        if (ReferenceEquals(other, null))
            return false;

        return (this.X == other.X && this.Y == other.Y);
    }

    public override bool Equals(object obj)
    {
        Point other = obj as Point;
        if (ReferenceEquals(other, null))
            return false;
        return Equals(other);
    }
}
like image 65
Lasse V. Karlsen Avatar answered Dec 16 '25 21:12

Lasse V. Karlsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!