namespace TestEqual
{
    class Program
    {
        static void Main(string[] args)
        {
            Point2D a = new Point2D
            {
                X = 1,
                Y = 2
            };

            Point2D b = a;

            CallEquals(a, b);
            a.Equals(b);
        }

        public static void CallEquals<T>(T instance, T other)
        {
            instance.Equals(other);
        }
    }

    public struct Point2D
    {
        public int X;
        public int Y;

        public override bool Equals(object obj)
        {
            if (!(obj is Point2D)) return false;
            Point2D other = (Point2D) obj;
            return X == other.X && Y == other.Y;
        }

        public bool Equals(Point2D other)
        {
            return X == other.X && Y == other.Y;
        }
    }
}
CallEquals(a, b);  走 public override bool Equals(object obj)
a.Equals(b); 走 public bool Equals(Point2D other)
如果 struct Point2D 继承 IEquatable  则都会走 public bool Equals(Point2D other) 可以避免一次装箱
 

相关文章:

  • 2022-12-23
  • 2021-06-06
  • 2022-02-19
  • 2021-12-23
  • 2021-06-15
  • 2022-12-23
猜你喜欢
  • 2021-05-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
  • 2022-01-26
  • 2021-05-24
相关资源
相似解决方案