c# 重载运算符(+-|&)和扩展方法
通常我们需要对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# 重载运算符(+-|&)和扩展方法的更多相关文章
- C#3.0新特性:隐式类型、扩展方法、自动实现属性,对象/集合初始值设定、匿名类型、Lambda,Linq,表达式树、可选参数与命名参数
一.隐式类型var 从 Visual C# 3.0 开始,在方法范围中声明的变量可以具有隐式类型var.隐式类型可以替代任何类型,编译器自动推断类型. 1.var类型的局部变量必须赋予初始值,包括匿名 ...
- C#高级功能(四)扩展方法和索引
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用.扩展方法被定义为静态方法,但 ...
- 从C过渡到C++的几个知识点(结构体、引用、重载运算符)
一.结构体和类(class) 下面一个使用结构体类型的例子 #include <iostream> using namespace std; struct Point{ // 声明Poin ...
- C++ 重载运算符 继承 多态 (超详细)
(一)重载运算符: (1)声明与定义格式 一般是类内声明,类外定义,虽然可以在类内定义,但 写前面堆一堆不好看!!! 类内声明: class Demo { 返回值类型 operator 运算符(形参表 ...
- .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类
.NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...
- C#中的扩展方法
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 以上是msdn官网对扩展方 ...
- .NET中那些所谓的新语法之二:匿名类、匿名方法与扩展方法
开篇:在上一篇中,我们了解了自动属性.隐式类型.自动初始化器等所谓的新语法,这一篇我们继续征程,看看匿名类.匿名方法以及常用的扩展方法.虽然,都是很常见的东西,但是未必我们都明白其中蕴含的奥妙.所以, ...
- 【开源】OSharp框架解说系列(3):扩展方法
OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...
- IEnumerable<T>与IQueryable<T>以及.net的扩展方法
首先看看继承关系 public abstract class DbSet : DbQuery public abstract class DbQuery : IOrderedQueryable, IQ ...
随机推荐
- html+css学习总结
HTML与css学习总结 一:html概念 1. html是一种描述网页的语言,并不是计算机语言这要分清楚:标记语言是运用一套标记标签描述网页的: 注意点: ①标签字母都要小写,标签一般都是成对出现, ...
- sql将表中的某个字段进行排序
. update tempTable set field1 = rownum from( select field1, ROW_NUMBER() over(order by fieldId) rown ...
- IOS添加控件
YJQApp *appInfo =self.apps[i]; //.添加图片 UIImageView * iconView = [[UIImageView alloc]init]; CGFloat i ...
- Cookie和Session简介与区别
1.Cookie和Session简介与区别 在非常多时候,我们需要跟踪浏览者在整个网站的活动,对他们身份进行自动或半自动的识别(也就是平时常说的网站登陆之类的功能),这时候,我们常采用Cookie与 ...
- windows下部署 ISCSI存储
Write bt xiaoyang 配置篇 这里使用的软件为iscsiTargetqfe 1. 首先安装软件,可在微软官网下载 2. 然后找到安装程序 3. 完成安装后打 ...
- OC3-xml文件解析
<?xml version="1.0"?> <xml_api_reply version="1"> <cities> < ...
- AMQ学习笔记 - 05. 客户端模板化
概述 客户端编程模型中,大部分的步骤都是相同的.将相同的部分做成模板,将不同的部分预留接口,实现者就只需要针对不同的部分提供实现. 设计 类图 发送方客户端 说明: 基于模板的思想,SendTempl ...
- IoC容器的初始化过程
1.简单来说,IoC容器的初始化是由前面介绍的refresh()方法来启动的,这个方法标志着IoC容器的正式启动. 2.具体来说,这个启动包括BeanDefinition的Resource定位.载入和 ...
- webSphere提示SSL证书过期,解决方法
1.点击Security ------SSL certificate and key management2.点击Related Items下的key stores and certificates3 ...
- HDU1022 Train Problem I 栈的模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 栈的模拟,题目大意是已知元素次序, 判断出栈次序是否合理. 需要考虑到各种情况, 分类处理. 常 ...