通常我们需要对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();
Test test2 = new Test();
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();
Test test2 = new Test();
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();
}
}
}

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

c# 重载运算符(+-|&)和扩展方法的更多相关文章

  1. C#3.0新特性:隐式类型、扩展方法、自动实现属性,对象/集合初始值设定、匿名类型、Lambda,Linq,表达式树、可选参数与命名参数

    一.隐式类型var 从 Visual C# 3.0 开始,在方法范围中声明的变量可以具有隐式类型var.隐式类型可以替代任何类型,编译器自动推断类型. 1.var类型的局部变量必须赋予初始值,包括匿名 ...

  2. C#高级功能(四)扩展方法和索引

    扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用.扩展方法被定义为静态方法,但 ...

  3. 从C过渡到C++的几个知识点(结构体、引用、重载运算符)

    一.结构体和类(class) 下面一个使用结构体类型的例子 #include <iostream> using namespace std; struct Point{ // 声明Poin ...

  4. C++ 重载运算符 继承 多态 (超详细)

    (一)重载运算符: (1)声明与定义格式 一般是类内声明,类外定义,虽然可以在类内定义,但 写前面堆一堆不好看!!! 类内声明: class Demo { 返回值类型 operator 运算符(形参表 ...

  5. .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类

    .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...

  6. C#中的扩展方法

    扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 以上是msdn官网对扩展方 ...

  7. .NET中那些所谓的新语法之二:匿名类、匿名方法与扩展方法

    开篇:在上一篇中,我们了解了自动属性.隐式类型.自动初始化器等所谓的新语法,这一篇我们继续征程,看看匿名类.匿名方法以及常用的扩展方法.虽然,都是很常见的东西,但是未必我们都明白其中蕴含的奥妙.所以, ...

  8. 【开源】OSharp框架解说系列(3):扩展方法

    OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...

  9. IEnumerable<T>与IQueryable<T>以及.net的扩展方法

    首先看看继承关系 public abstract class DbSet : DbQuery public abstract class DbQuery : IOrderedQueryable, IQ ...

随机推荐

  1. 使用Jeditable插件时遇到的问题

    Jeditable在渲染页面已有DIV=>form的时候 首先使用 $("div").html(); 去获取原DIV中的内容. 这样导致一个问题, 如果原div中带有html ...

  2. jquery简单的大背景banner图片全屏切换

    详细内容请点击 这个是我初毕业刚进公司那会帮同事(同时也是同学)写的一个PC端的全屏图片切换效果,对于刚毕业的我来说写出来那会的喜悦之情是无法言表的,那时的我还是什么不懂的小白白,俗称菜鸟.个人网站上 ...

  3. 【Mongodb】---关联表查询population

    Population MongoDB是非关联数据库.但是有时候我们还是想引用其它的文档.这就是population的用武之地. Population是从其它文档替换文档中的特定路径.我们可以迁移一个单 ...

  4. PHP和CS的引用传值

    PHP的引用传值 function change_value($num){ $num+=2; } $age = 3; change_value(&$age); echo $age; CS的引用 ...

  5. Android—SDCard数据存取&Environment简介

    1:Environment简介: Environment是android.os包下的一个类,谷歌官方文旦的解释为:Provides access to environment variables(提供 ...

  6. 更改Activity亮度

    有些需求需进入到页面后更改Activity的亮度,退出页面后恢复到之前的亮度.通过更改WindowManager.LayoutParams的screenBrightness可以达到这个效果.scree ...

  7. hdu 1222 Wolf and Rabbit

    Problem Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbi ...

  8. 删除utorrent广告栏

    自从utorrent附带广告栏后整个页面很难看,特别是右面下载详细列表变得支离破碎,今天实在是忍不了,网络搜索关键字“remove utorrent ads”,有效解决了问题. 具体如下:打开设置,选 ...

  9. Linux命令(2):ls命令

    1.作用:列出目录的内容: 2.格式:ls [选项] [文件] [选项]为指定要查看文件相关的内容,若未指定文件默认查看当前目录下的所有文件: 3.常见参数: 如图: 4.使用实例: [yournam ...

  10. 20150222—LINQ to SQL 插入、更新和删除

    注意,使用LINQ to SQL时, 表中必须有一个主键才可以起效,否则系统将无法对数据作出修改 插入新数据,根据上一片的文章实例在其中添加新的控件: 编号TextBox(Name):sno 名字Te ...