C#操作符重载

namespace 操作符重载
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var a = new Test(11);

            var b = new Test(12);

            var c = a + b;

            Console.WriteLine(c.Lenght);

            Console.ReadKey();
        }
    }

    internal class Test
    {
        public Test(int length)
        {
            Lenght = length;
        }

        public int Lenght { get; set; }

        public static Test operator +(Test test1, Test test2)
        {
            return new Test(test1.Lenght + test2.Lenght);
        }
    }
}

 

posted @ 2021-12-11 14:58  鸟飞好样的  阅读(110)  评论(0编辑  收藏  举报