Home > Development > Microsoft .NET Equality

Microsoft .NET Equality


If you need an example of implementing equality here is a contrived demo on it.

using System;

namespace EqualityExamples
{

    public class Point // Refence types only eqaul by default if they refer to the same object on the heap.
    {
        private readonly int _x;
        private readonly int _y;

        public Point(int x, int y)
        {
            this._x = x;
            this._y = y;
        }

        public int X
        {
            get { return _x; }
        }

        public int Y
        {
            get { return _y; }
        }
    }

    public class EPoint
    {
        private readonly int _x;
        private readonly int _y;

        public EPoint(int x, int y)
        {
            this._x = x;
            this._y = y;
        }

        public int X
        {
            get { return _x; }
        }

        public int Y
        {
            get { return _y; }
        }

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

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

    }

    public class FPoint
    {
        private readonly int _x;
        private readonly int _y;

        public FPoint(int x, int y)
        {
            this._x = x;
            this._y = y;
        }

        public int X
        {
            get { return _x; }
        }

        public int Y
        {
            get { return _y; }
        }

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

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

        public override bool Equals(object obj)
        {
            FPoint p = obj as FPoint;
            
            if (ReferenceEquals(p, null)) // Compares references are the same - prevents stack overflow
                                          // checks that the object has a value otherwise its false
                return false;

            if (!Equals(X, p.X)) // Compares the X property using the static Equals method
                return false;

            return Equals(Y, p.Y); // Compares the Y property using the static Equals method
        }

    }

    public class Program
    {
        public static void Main()
        {

            Point p1 = new Point(1,2);
            Point p2 = new Point(1,2);

            Console.WriteLine("Equal: {0}", p1.Equals(p2)); // Refence types only eqaul by default if they refer to the same object on the heap.
            Console.WriteLine("Equal: {0}", Equals(p1, p2)); // Refence types only eqaul by default if they refer to the same object on the heap.
            Console.WriteLine("Equal: {0}", p1 == p2);// Uses the default == operator comparing by reference so not sematically correct

            EPoint e1 = new EPoint(1,2);
            EPoint e2 = new EPoint(1, 2);

            Console.WriteLine("Equal: {0}", e1.Equals(e2));  // Refence types only eqaul by default if they refer to the same object on the heap.
            Console.WriteLine("Equal: {0}", Equals(e1, e2)); // Refence types only eqaul by default if they refer to the same object on the heap.
            Console.WriteLine("Equal: {0}", e1 == e2); // Uses Overload operator which now defines what equality means

            FPoint f1 = new FPoint(1, 2);
            FPoint f2 = new FPoint(1, 2);

            Console.WriteLine("Equal: {0}", f1.Equals(f2)); // Now sematically correct
            Console.WriteLine("Equal: {0}", Equals(f1, f2)); // Now sematically correct 
            Console.WriteLine("Equal: {0}", f1 == f2);  // Uses Overload operator which now defines what equality means

            Console.ReadKey();
        }
    }
}

Blair…

Advertisements
Categories: Development Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: