码迷,mamicode.com
首页 > Windows程序 > 详细

C# 运算符重载

时间:2016-10-03 14:42:51      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

C#运算符重载函数必须是public static

    struct CSTest
    {
        public int posx;
        public static CSTest operator + (CSTest o1, CSTest o2)
        {//二元运算符重载
            CSTest ost = new CSTest();
            ost.posx = o1.posx + o2.posx;
            return ost;
        }
        public static CSTest operator - (CSTest ot)
        {//一元运算符重载
            ot.posx = -ot.posx;
            return ot;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            CSTest ostn1 = new CSTest();
            CSTest ostn2 = new CSTest();
            ostn1.posx = 1;
            ostn2.posx = 2;
            CSTest ostn3 = ostn1 + ostn2;
            ostn3 = -ostn3;
            Console.WriteLine(ostn3.posx);
        }
    }

C# 运算符重载

标签:

原文地址:http://www.cnblogs.com/timeObjserver/p/5928984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!