How to define the operator ==
<p>Given the class as follows,</p>

<pre><code>public class Number
{
  public int X { get; set; }
  public int Y { get; set; }
}
</code></pre>

<p>How to define an overload operator <code>==</code> so that I can use the following statement:</p>

<pre><code>Number n1 = new Number { X = 10, Y = 10 };
Number n2 = new Number { X = 100, Y = 100 };

if (n1 == n2)
    Console.WriteLine("equal");
else
    Console.WriteLine("not-equal");
</code></pre>

<p><strong>// Updated based on comments as follows //</strong></p>

<p>Here is a further question:
It seems to me that C# does operator overload differently from that of C++.
In C++, this overloaded operator is defined outside of the targeting class as a standalone function. Here in C#, this overload function in fact is embedded into the targeting class.</p>

<p>Can someone give me some comments on this topic?</p>

<p>Thank you</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    public class Number
    {
        public int X { get; set; }
        public int Y { get; set; }

        public Number() { }
        public Number(int x, int y)
        {
            X = x;
            Y = y;
        }

        public static bool operator==(Number a, Number b)
        {
            return ((a.X == b.X) &amp;&amp; (a.Y == b.Y));
        }

        public static bool operator !=(Number a, Number b)
        {
            return !(a == b);
        }

        public override string ToString()
        {
            return string.Format("X: {0}; Y: {1}", X, Y);
        }

        public override bool Equals(object obj)
        {
            var objectToCompare = obj as Number;
            if ( objectToCompare == null )
                return false;

            return this.ToString() == obj.ToString();
        }

        public override int GetHashCode()
        {
            return this.ToString().GetHashCode();
        }

    }

    class Program
    {
        static void Main(string[] arg)
        {
            Number n1 = new Number { X = 10, Y = 10 };
            Number n2 = new Number { X = 10, Y = 10 };

            if (n1 == n2)
                Console.WriteLine("equal");
            else
                Console.WriteLine("not-equal");

            Console.ReadLine();
        }
    }
}
</code></pre>
