• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c# 重载运算符(+-|&)和扩展方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
通常我们需要对class的相加,相减,相乘 等重载以适应需求, 如caml查询的时候,我们可以定义一个caml类,然后来操作这些查询.

首先,我们定义一个class为Test
public class Test

 然后定义两个成员,一个int类型的ID,一个字符串类型的Name.

        public int ID;
        public string Name;

然后定义构造函数

        public Test()
        {
        }

        public Test(int id)
        {
            this.ID = id;
        }

        public Test(int id, string name)
        {
            this.ID = id;
            this.Name = name;
        }

重载两个class相加的运算符,

        public static Test operator +(Test t1, Test t2)
        {
            if (t2.Name!= null)
            {
                return new Test(t1.ID + t2.ID, t1.Name + t2.Name);
            }
            else
            {
                return new Test(t1.ID + t2.ID);
            }
        }

重载两个class的|运算,其他的运算符如(-,*,/,&)大家可以自己去试试.

        public static Test operator |(Test t1, Test t2)
        {
            //显示ID大的class
            return new Test(t1.ID > t2.ID ? t1.ID:t2.ID);
        }

下面写了一个对Test这个class的扩展方法,相等于这个class自带的成员方法. 扩展返回发的写法关键是this 后面带类型参数

    internal static class Util
    {
        public static string Format(this Test t)
        {
            StringBuilder sb = new StringBuilder();
            if (t.ID != null)
            {
                sb.AppendLine("ID:"+t.ID.ToString());
            }
            if (!string.IsNullOrEmpty(t.Name))
            {
                sb.AppendLine("Name:" + t.Name.ToString());
            }
            return sb.ToString();
        }
    }

 

调用这个方法:

    class Program
    {
        static void Main(string[] args)
        {
            //测试两个class相加
            Test test1 = new Test(1);
            Test test2 = new Test(2);
            Console.WriteLine("两个class相加的结果为:"+(test1 +test2).Format());
            Console.WriteLine("两个class比较值大的结果为:" + (test1 |test2).Format());
            
        }
    }

 

运行结果如下:

 

全部代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Charlie.ConsoleWindow
{
    class Program
    {
        static void Main(string[] args)
        {
            //测试两个class相加
            Test test1 = new Test(1);
            Test test2 = new Test(2);
            Console.WriteLine("两个class相加的结果为:"+(test1 +test2).Format());
            Console.WriteLine("两个class比较值大的结果为:" + (test1 |test2).Format());
            
        }
    }

    public class Test
    {
        public int ID;
        public string Name;

        public Test()
        {
        }

        public Test(int id)
        {
            this.ID = id;
        }

        public Test(int id, string name)
        {
            this.ID = id;
            this.Name = name;
        }

        public static Test operator +(Test t1, Test t2)
        {
            if (t2.Name!= null)
            {
                return new Test(t1.ID + t2.ID, t1.Name + t2.Name);
            }
            else
            {
                return new Test(t1.ID + t2.ID);
            }
        }

        public static Test operator |(Test t1, Test t2)
        {
            //显示ID大的class
            return new Test(t1.ID > t2.ID ? t1.ID:t2.ID);
        }
    }

    internal static class Util
    {
        public static string Format(this Test t)
        {
            StringBuilder sb = new StringBuilder();
            if (t.ID != null)
            {
                sb.AppendLine("ID:"+t.ID.ToString());
            }
            if (!string.IsNullOrEmpty(t.Name))
            {
                sb.AppendLine("Name:" + t.Name.ToString());
            }
            return sb.ToString();
        }
    }
}
View Code

 

如有错误,请大家指正~~~~

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#处理XML!发布时间:2022-07-13
下一篇:
《CLR via C#》笔记——线程基础发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap