How to define the operator ==

后端 未结 4 537
执笔经年
执笔经年 2021-01-22 21:37

Given the class as follows,

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

How to define an overload operato

4条回答
  •  无人及你
    2021-01-22 22:03

    Here is a short example of how to do it, but I very strongly recommend reading Guidelines for Overloading Equals() and Operator == (C# Programming Guide) to understand the interaction between Equals() and == and so on.

    public class Number
    {
      public int X { get; set; }
      public int Y { get; set; }
    
      public static bool operator ==(Number  a, Number b)
      {
        // TODO
      }
    }
    

提交回复
热议问题