c#-运算符重载-等号运算符

using System;

// c#-运算符重载-等号运算符
namespace ConsoleApp10
{
    class A {
        public A(int b) => this.b = b;
        public int b;
        public static bool operator ==(A left, A right) {
            return (left.b == right.b) ;
        }
        public static bool operator !=(A left, A right)
        {
            return !(left == right);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            A a = new A(5);
            A b = new A(5);
            A c = new A(6);
            Console.WriteLine(a == b);
            Console.WriteLine(a == c);
        }
    }
}

c#-运算符重载-等号运算符_第1张图片

你可能感兴趣的:(c#)