如何定义运算符 ==

本文关键字:运算符 定义 何定义 | 更新日期: 2023-09-27 17:56:13

给定的类如下,

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

如何定义重载运算符==以便我可以使用以下语句:

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");

根据评论更新如下//

这里还有一个问题:在我看来,C# 的运算符重载与 C++ 的运算符重载不同。在C++中,此重载运算符在目标类外部定义为独立函数。在 C# 中,此重载函数实际上嵌入到目标类中。

有人可以给我一些关于这个话题的评论吗?

谢谢

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) && (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();
        }
    }
}

如何定义运算符 ==

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

更多信息在这里。总之:

  • 重载运算符 == 的任何类型也应该重载运算符 !=
  • 重载运算符 == 的任何类型也应该覆盖 Equals
  • 建议任何覆盖 Equals 的类也覆盖 GetHashCode

来自 MSDN 文档:

public static bool operator ==(ThreeDPoint a, ThreeDPoint b)
{
    // If both are null, or both are same instance, return true.
    if (System.Object.ReferenceEquals(a, b))
    {
        return true;
    }
    // If one is null, but not both, return false.
    if (((object)a == null) || ((object)b == null))
    {
        return false;
    }
    // Return true if the fields match:
    return a.x == b.x && a.y == b.y && a.z == b.z;
}
public static bool operator !=(ThreeDPoint a, ThreeDPoint b)
{
    return !(a == b);
}

这是一个如何做到这一点的简短示例,但我强烈建议阅读重载 Equals() 和运算符 ==(C# 编程指南)的指南,以了解 Equals() 和 == 之间的交互等等。

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

自己重载运算符:

public static bool operator ==(Number a, Number b)
{
    // do stuff
    return true/false;
}