C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针。委托(Delegate) 是存有对某个方法的引用的一种引用类型变量。引用可在运行时被改变。

委托(Delegate)特别用于实现事件和回调方法。所有的委托(Delegate)都派生自 System.Delegate 类。

委托的声明(没有方法体的函数加上delegate关键字):

        /// <summary>
/// 无参委托
/// </summary>
public delegate void DelegaetNoReturnPara();
/// <summary>
/// 有参委托
/// </summary>
/// <param name="num"></param>
public delegate void DelegatePara(int num);
/// <summary>
/// 有参带返回值的委托
/// </summary>
/// <param name="num"></param>
public delegate int DelegateParaNumber(int num);
/// <summary>
/// 泛型委托
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public delegate T DelegateGeneric<T>(T t);

我们用反编译工具查看一下

用反编译工具可以看到委托是一个密闭的方法并且继承于[System.Runtime]System.MulticastDelegate,委托有一个构造函数,带有Invoke、BeginInvoke、EndInvoke三个方法。

委托既然是类,那我们也可对其进行实例化

        public static void GetData(int num)
{
Console.WriteLine($"调用了{nameof(GetData)}方法");
} public delegate void DelegatePara(int num); static void Main(string[] args)
{
Customer.GetData();
//委托的实例化必须传递一个方法,这个方法的返回值和参数必须和实例化的这个委托一致
//把方法包装成变量,Invoke的时候自动执行方法
DelegatePara noReturnPara = new DelegatePara(Customer.GetData);
noReturnPara.Invoke(1);//调用委托
noReturnPara(1);
}

在这里可能有同学会想,为什么不直接调用方法呢?ok,接着往下看.

有一个student类和一个获取集合对象的一个方法.

public class Student
{
public string name { get; set; }
public int age { get; set; }
public string sex { get; set; }
} public async Task<List<Student>> GetAllStudents()
{
var result = new List<Student>();
for (int i = ; i < ; i++)
{
result.Add(new Student
{
age = + i,
name = $"橘猫{i}",
sex = i / == ? "男" : "女"
});
}
return result;
}

假如现在有三个判断条件筛选集合中的内容

/// <summary>
/// 查询name的长度大于2的同学
/// </summary>
/// <returns></returns>
public async Task<List<Student>> GetStudents()
{
var result = new List<Student>();
var data = await this.GetAllStudents();
foreach (var item in data)
{
if (item.name.Length > )
result.Add(item);
}
return result;
}
/// <summary>
/// 查询年龄大于18岁的同学
/// </summary>
/// <returns></returns>
public async Task<List<Student>> GetStudents()
{
var result = new List<Student>();
var data = await this.GetAllStudents();
foreach (var item in data)
{
if (item.age > )
result.Add(item);
}
return result;
}
/// <summary>
/// 查询年龄大于18岁并且是女生的同学
/// </summary>
/// <returns></returns>
public async Task<List<Student>> GetStudents()
{
var result = new List<Student>();
var data = await this.GetAllStudents();
foreach (var item in data)
{
if (item.age > && item.sex.Equals("女"))
result.Add(item);
}
return result;
}

三个判断条件要写三个方法是不是太冗余了,我们直接写在一个方法里,根据条件类型来筛选数据

/// <summary>
/// 根据type来查询
/// </summary>
/// <returns></returns>
public async Task<List<Student>> GetStudents(int type)
{
var result = new List<Student>();
var data = await this.GetAllStudents();
switch (type)
{
case :
foreach (var item in data)
{
if (item.name.Length > )
result.Add(item);
}
break;
case :
foreach (var item in data)
{
if (item.age > )
result.Add(item);
}
break;
case :
foreach (var item in data)
{
if (item.age > && item.sex.Equals("女"))
result.Add(item);
}
break;
default:
Console.WriteLine("查询的类型有误,请重新输入");
break;
}
return result;
}

这样的话行是行,但是如果在多加一个条件呢?那么我们可不可以将判断逻辑传到方法里,只需返回true或false就行了.

//定义委托
public delegate bool judge(Student student);

//用来判断姓名
public bool judgeName(Student student)
{
return student.name.Length > ;
}
//用来判断年龄
public bool judgeAge(Student student)
{
return student.age > ;
}
//用来判断年龄和性别
public bool judgeAgeAndSex(Student student)
{
return student.age > && student.sex.Equals("女");
}
/// <summary>
/// 将判断逻辑传递过来
/// 委托解耦,减少重复代码,将公共逻辑当成变量传递过来
/// </summary>
/// <returns></returns>
public async Task<List<Student>> GetStudents(judge judge)
{
var result = new List<Student>();
var data = await this.GetAllStudents();
foreach (var item in data)
{
if (judge.Invoke(item))
result.Add(item);
}
return result;
}

有同学会说,你这代码也不精简,为什么不用lamda.这里的话主要是想告诉大家委托的作用,侧重点不同,望理解。

多播委托:

public delegate void DelegatePara(int num);
//这个委托实例只包含一个方法
DelegatePara noReturnPara = new DelegatePara(Customer.GetData);
//+= 为委托实例按顺序增加方法,形成方法链,依次执行
DelegatePara noReturnPara = new DelegatePara(Customer.GetData);
noReturnPara += new DelegatePara(Customer.GetData);
noReturnPara += new DelegatePara(new DelegateInstance().GetData);
noReturnPara.Invoke();//调用委托
//-= 为委托实例移除方法,从方法链的底部开始匹配,遇到第一个完全吻合的,移除且只移除一个
noReturnPara += new DelegatePara(Customer.GetData);
noReturnPara += new DelegatePara(new DelegateInstance().GetData);//这个去不掉,因为不是同一个实例中的方法
//循环获得委托中的实例
foreach (var item in noReturnPara.GetInvocationList())
{
item.DynamicInvoke();//调用委托
}

多播委托如果带返回值,结果以最后的为准。

事件:是带event关键字的委托实例,event可以限制变量被外部调用/直接赋值

public delegate void HandleEvent(int num);
public event HandleEvent handleEvent;
public void DelegateEvent()
{
handleEvent = new HandleEvent(Customer.GetData);
handleEvent.Invoke();
}

如果不是在本身所在的类中调用(事件)会如何呢?

很明显报错了

只能像下面这样写

DelegateInstance delegateInstance = new DelegateInstance();
delegateInstance.handleEvent += new DelegateInstance.HandleEvent(Customer.GetData);
Console.ReadKey();

这里需要说一下:Event+委托的一个实例,加上一个event关键字限制了外部调用权限,保证其安全(不能再其它类中调用和赋值,只能用来添加和移除注册方法),在本身类中可以调用和赋值,但是在子类中也不可以调用和赋值.

委托与事件的区别和联系:委托是一个类型(委托的本质是一个类);事件是委托类型的一个实例.

ok,今天先到这儿。如有不足的地方,望见谅。

C# 委托浅析的更多相关文章

  1. C#委托及事件处理机制浅析

    事件可以理解为某个对象所发出的消息,以通知特定动作(行为)的发生或状态的改变.行为的发生可能是来自用户交互,如鼠标点击:也可能源自其它的程序逻辑.在这里,触发事件的对象被称为事件(消息)发出者(sen ...

  2. 浅析C#之委托、Action、Func

    一.委托 1.1 委托的定义 delegate(委托)是一种可用于封装命名方法或匿名方法的引用类型, 委托类似于 C++ 中的函数指针: .Net通过委托来提供回调函数机制. 声明一个委托类型 int ...

  3. c#委托之浅析

    前言: 这章我们将弄懂,委托是什么?有什么作用?在什么样的场景下可以启到什么作用? 委托适用的场景:当确定处理一个任务时,不确定其处理任务的方法时可使用,这样可以提高扩展性,调用符合条件的处理方法,避 ...

  4. 读书笔记 C#委托的BeginInvoke、EndInvoke之浅析

    c#中有一种类型叫委托,它是一种引用类型.可以引用静态与非静态的方法,且这些方法的参数列表和返回值类型必须与所声明的委托一致. 委托引用的方法可以通过BeginInvoke和EndInvoke来异步进 ...

  5. c#进阶之浅析委托和事件

    何为委托 加了delegate关键字,没有方法体{}的方法模版(方法列表);委托是一种类型 public void Write() { //TODO } //加上关键字delegate,去掉方法体{} ...

  6. C#回调浅析(委托、接口回调)

    https://wenku.baidu.com/view/ed724173bd64783e08122b2f.html

  7. 【深入浅出jQuery】源码浅析2--奇技淫巧

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  8. Reactive Extensions(Rx)并发浅析

    Reactive Extensions(Rx)并发浅析 iSun Design & Code .Net并行编程 - Reactive Extensions(Rx)并发浅析 关于Reactive ...

  9. C# Winform窗口之间传值的多种方法浅析(转)

    摘要http://www.jb51.net/article/63837.htm 这篇文章主要介绍了C# Winform窗口之间传值的多种方法浅析,本文起讲解了通过构造器传值.通过属性传递.通过事件携带 ...

随机推荐

  1. php5与php7安全性的区别

    0X01 前言 本篇文章大多为转载,但是修正了一些不正确的说法,对某些功能点的变更指出具体是哪个版本变更,加入了一些小更新. (原文地址:https://www.freebuf.com/article ...

  2. spring中bean的常用属性

    一.scop scope用来配置bean对象是否是单例模式.单例模式是java的二十三种设置模式之一,指在这个项目运行过程中一 个类的对象只会实例化一次.一般,工厂类的对象都是单例模式.非单例模式叫多 ...

  3. blesta运行造成阿里云服务器CPU频繁超载的原因分析

    博主在阿里云服务器上安装了主机软件Blesta后,阿里云后台频繁提示CPU超载,打开突发性能模式后,发现CPU负载到了100%.如下图所示: 直接在putty里面reboot整个系统后,负载瞬间降为2 ...

  4. [hdu2112]最短路

    相当于模板题了,用trie来完成字符串到数字的映射比map<string, int>要快不少,令外可以考虑hash. 运行时间对比: (1)(2)600ms左右 (3)3000ms左右(4 ...

  5. 《 .NET并发编程实战》一书中的节流为什么不翻译成限流

    有读者问,为什么< .NET并发编程实战>一书中的节流为什么不翻译成限流? 这个问题问得十分好!毕竟“限流”这个词名气很大,耳熟能详,知名度比“节流”大多了. 首先,节流的原词Thrott ...

  6. 分布式项目配置工程,在项目间互通要先在linux下开启zookeeper

    一.编写配置文件 <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http: ...

  7. 十分钟看懂AES加密

    十分钟看懂AES加密算法 今天看了Moserware的<A Stick Figure Guide to the Advanced Encryption Standard(AES)>收获了不 ...

  8. sonar 安装,centos7配置优化

    /etc/sysctl.conf /etc/systemd/system.conf /etc/security/limits.conf /proc/sys/fs/file-max /etc/secur ...

  9. 【雕爷学编程】Arduino动手做(44)---类比霍尔传感器

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...

  10. vue中使用mixins

    Mixins (混合或混入)——定义的是一个对象 1.概念:一种分发Vue组件可复用功能的非常灵活的方式.混入对象可以包含任意组件选项(组件选项:data.watch.computed.methods ...