How to define the operator ==

后端 未结 4 531
执笔经年
执笔经年 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:15

    public static bool operator ==(YourClassType a, YourClassType b)
    {
        // do the comparison
    }
    

    More information here. In short:

    • Any type that overloads operator == should also overload operator !=
    • Any type that overloads operator == should also should override Equals
    • It is recommended that any class that overrides Equals also override GetHashCode

提交回复
热议问题