一、什么是委托:
委托是寻址方法的.NET版本,使用委托可以将方法作为参数进行传递。委托是一种特殊类型的对象,其特殊之处在于委托中包含的只是一个活多个方法的地址,而不是数据。
 
二、使用委托: 关键字:delegate
1.声明:
      public delegate void DoNothing();//定义一个无返回值,无参数的委托
     public delegate int GetNum(int i); //定义有一个返回值int ,参数int 的委托
2.创建方法:
public static void DoSome()//无参无返回值的方法
{
Console.WriteLine("DoSome");
}
public static int TotalNum(int num)//有一个返回值int ,参数int 的方法
{
return num * num;
}
 
3.注册委托:
DoNothing doNothing = new DoNothing(DoSome);
//或者直接写出DoNothing doNothing = DoSome;
 
GetNum getNum = AddNum;//注册委托
 
4.执行委托
doNothing.Invoke();//执行委托  也可以直接 doNothing();
Console.WriteLine(getNum.Invoke(10));//执行委托并且打印
 
三、委托的意义
传递方法;把方法包裹起来, 传递逻辑。异步多线程执行
 
四、.net framework3.5之后,系统定义好了2个委托,开发尽量使用框架自带委托,尽量使用Action和Func
Action 无返回值委托,Func 有返回值委托
 
Action要使用参数,就写Action<int,string,double> 最多可以到16个
 
Func要使用参数,就写成Func<int,string,double> 最多可以到17个, 最后一个为返回值,现在这个返回的就是double类型
 
Action act = DoSome;//Action 无返回值委托
act.Invoke();
 
 Func<int,int> func = new Func<int,int>(TotalNum)  ;
func(10);
 
 
五、多播委托
Action doSome = new Action(DoSome);
doSome += new Action(DoSome);
doSome += DoSome;
 
doSome();//按顺序执行,最后结果是执行3次DoSome方法
 
doSome -= DoSome;//减少一次DoSome执行
 
doSome();//按顺序执行,最后结果是执行2次DoSome方法
 
多播委托,按顺序执行,多播委托,用Action, Func带返回值的只执行完后,只得到最后一个结果,所以没有意义。
 
委托使用案例:一个学生类,一个学生管理静态类,可以通过委托,实现学生集合的筛选
  1. public class Student
  2. {
  3. public int Id { get; set; }
  4.  
  5. public string Name { get; set; }
  6.  
  7. public int ClassId { get; set; }
  8.  
  9. public int Age { get; set; }
  10. }
  11.  
  12. public static class StudentManager
  13. {
  14. public static List<Student> students = new List<Student>()
  15. {
  16. new Student(){ Id=,Name="张三",ClassId=,Age= },
  17. new Student(){ Id=,Name="李四",ClassId=,Age= },
  18. new Student(){ Id=,Name="王五",ClassId=,Age= },
  19. new Student(){ Id=,Name="赵六",ClassId=,Age= },
  20. new Student(){ Id=,Name="杨幂",ClassId=,Age= },
  21. new Student(){ Id=,Name="范冰冰",ClassId=,Age= },
  22. new Student(){ Id=,Name="张学友",ClassId=,Age=},
  23. new Student(){ Id=,Name="张三1",ClassId=,Age= },
  24. new Student(){ Id=,Name="张三2",ClassId=,Age= },
  25. new Student(){ Id=,Name="张三3",ClassId=,Age= },
  26. new Student(){ Id=,Name="张三4",ClassId=,Age= },
  27. new Student(){ Id=,Name="张三5",ClassId=,Age= },
  28. new Student(){ Id=,Name="张三6",ClassId=,Age= },
  29. new Student(){ Id=,Name="张三7",ClassId=,Age= },
  30. new Student(){ Id=,Name="张三8",ClassId=,Age= },
  31. new Student(){ Id=,Name="张三9",ClassId=,Age= },
  32. new Student(){ Id=,Name="张三0",ClassId=,Age= },
  33. new Student(){ Id=,Name="张三11",ClassId=,Age= },
  34. new Student(){ Id=,Name="张三a",ClassId=,Age= },
  35. new Student(){ Id=,Name="张三b",ClassId=,Age= },
  36. new Student(){ Id=,Name="张三c",ClassId=,Age= },
  37. new Student(){ Id=,Name="张三d",ClassId=,Age= },
  38. new Student(){ Id=,Name="张三e",ClassId=,Age= },
  39. new Student(){ Id=,Name="张三f",ClassId=,Age= },
  40. new Student(){ Id=,Name="张三g",ClassId=,Age= },
  41. new Student(){ Id=,Name="张三h",ClassId=,Age= },
  42. new Student(){ Id=,Name="张三i",ClassId=,Age= },
  43. new Student(){ Id=,Name="张三j",ClassId=,Age= },
  44. new Student(){ Id=,Name="张三k",ClassId=,Age= },
  45. };
  46.  
  47. public static List<Student> FindStudents(Func<Student,bool> func)
  48. {
  49. List<Student> stus = new List<Student>();
  50.  
  51. foreach (var item in students)
  52. {
  53. if (func(item))
  54. {
  55. stus.Add(item);
  56. }
  57. }
  58. return stus;
  59. }
  60.  
  61. /// <summary>
  62. /// 查找ClassId为3001的学生
  63. /// </summary>
  64. /// <param name="student">学生</param>
  65. /// <returns>是否为3001班级的学生</returns>
  66. public static bool GetClassId(Student student)
  67. {
  68. if (student.ClassId==)
  69. {
  70. return true;
  71. }
  72.  
  73. return false;
  74.  
  75. }
  76. /// <summary>
  77. /// 年龄大于20的学生
  78. /// </summary>
  79. /// <param name="student"></param>
  80. /// <returns></returns>
  81. public static bool GetBigAge(Student student)
  82. {
  83. if (student.Age>)
  84. {
  85. return true;
  86. }
  87. return false;
  88. }
  89. /// <summary>
  90. /// 年龄大于15 并且ClassId为1021
  91. /// </summary>
  92. /// <param name="student"></param>
  93. /// <returns></returns>
  94. public static bool GetStuByClassIdAndAge(Student student)
  95. {
  96. if (student.Age > && student.ClassId==)
  97. {
  98. return true;
  99. }
  100. return false;
  101. }
  102.  
  103. }

下面这个是在Main方法中执行查询学生

  1. //List<Student> stus = StudentManager.students;
  2.  
  3. //Console.WriteLine("姓名---年龄---班级--编号");
  4. //foreach (var item in stus)
  5. //{
  6. // Console.WriteLine(item.Name+"---"+item.Age+"---"+item.ClassId+"---"+item.Id);
  7. //}
  8.  
  9. List<Student> stus1= StudentManager.FindStudents(StudentManager.GetStuByClassIdAndAge);
  10.  
  11. Console.WriteLine("姓名---年龄---班级--编号");
  12. foreach (var item in stus1)
  13. {
  14. Console.WriteLine(item.Name + "---" + item.Age + "---" + item.ClassId + "---" + item.Id);
  15. }

.Net 委托 delegate 学习的更多相关文章

  1. IOS开发使用委托delegate在不同窗口之间传递数据

    IOS开发使用委托delegate在不同窗口之间传递数据是本文要介绍的内容,主要是来讲解如何使用委托delegate在不同窗口之间传递数据,具体内容来看详细内容.在IOS开发里两个UIView窗口之间 ...

  2. [.NET] C# 知识回顾 - 委托 delegate (续)

    C# 知识回顾 - 委托 delegate (续) [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6046171.html 序 上篇<C# 知识回 ...

  3. [C#] C# 知识回顾 - 委托 delegate

    C# 知识回顾 - 委托 delegate [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6031892.html 目录 What's 委托 委托的属性 ...

  4. C# 委托Delegate(一) 基础介绍&用法

    本文是根据书本&网络 前人总结的. 1. 前言 定义&介绍: 委托Delegate是一个类,定义了方法的类型, 使得可以将方法当做另一个方法的参数来进行传递,这种将方法动态地赋给参数的 ...

  5. 为什么不能把委托(delegate)放在一个接口(interface)当中?

    stackoverflow上有人问,为什么不能把委托放在一个接口当中? 投票最多的第一个答案第一句话说,“A Delegate is just another type, so you don't g ...

  6. C# 代理/委托 Delegate

    本文转载自努力,努力,努力 1. 委托的定义:委托是函数的封装,它代表一"类"函数.他们都符合一定的签名:拥有相同的参数列表,返回值类型.同时,委托也可以看成是对函数的抽象,是函数 ...

  7. c# 委托 delegate

    委托是一种存储函数引用的类型,在事件和事件的处理时有重要的用途 通俗的说,委托是一个可以引用方法的类型,当创建一个委托,也就创建一个引用方法的变量,进而就可以调用那个方法,即委托可以调用它所指的方法. ...

  8. 理解委托(delegate)及为什么要使用委托

    理解委托(delegate)及为什么要使用委托 委托:是一种定义方法签名的类型. 当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联. 您可以通过委托实例调用方法. 上述为官方说法,理解起来 ...

  9. 深入理解委托(Delegate)

    前言 委托其实一直以来都感觉自己应该挺熟悉的,直到最近又去翻了翻 CLR via C#,感觉我之前的理解可能还有失偏颇.在这记录一下. 之前文章的链接: 接口和委托的泛型可变性 C#高级编程笔记 De ...

随机推荐

  1. nodejs-5.2 axios请求

    1.npm官方文档:https://www.npmjs.com/package/axios 2.axios:用于 浏览器 和 node.js的基于Promise的HTTP客户端 请求 特征 从浏览器制 ...

  2. java中八大基本数据类型详解

    1.基本数据类型的分类 java中的类型分为基本数据类型和引用类型,今天我们讨论的是java中的八大基本数据类型. 基本数据类型可以分为三类:1.数值类型.2.字符类型.3.布尔类型. 数值类型又分为 ...

  3. C++的代理类

    怎样在一个容器中包含类型不同,但是彼此有关系的对象?众所周知,C++的容器只能存放类型相同的元素,所以直接在一个容器中存储不同类型的对象本身是不可能的,只能通过以下两种方案实现: 1. 提供一个间接层 ...

  4. Spark学习之编程进阶总结(一)

    一.简介 这次介绍前面没有提及的 Spark 编程的各种进阶特性,会介绍两种类型的共享变量:累加器(accumulator)与广播变量(broadcast variable).累加器用来对信息进行聚合 ...

  5. Visio打开或取消箭头的自动吸附和自动连接

    在用Visio画图时Visio的自动对齐.自动连接.自动吸附功能确实能带了很多便利.但在画连接线时,Visio总是自动连接箭头与图形的固定节点,想要微调一下连接位置,就显得很不方便,需要关闭自动连接功 ...

  6. Redis--Memched--Cache缓存介绍使用

    目录:  一.分布式缓存—Redis与Memched的区别 1.1.      数据支持类型 1.2.      持久性 1.3.      内存利用情况 1.4.      数据一致性 1.5.   ...

  7. idea配github

    下面步骤的三个前提条件:安装git 安装idea 注册github账号 步骤一.绑定我的github账号与我的计算机 绑定我的github账号与我的计算机之后,便能很方便地上传或者更新我的代码,这需要 ...

  8. 20180726 - Windows 10 Pro 下远程桌面连接提示“出现身份验证错误”

    问题:Windows 10 Pro 下远程桌面连接提示“出现身份验证错误” [Window Title]远程桌面连接 [Content]出现身份验证错误.要求的函数不受支持 远程计算机: 192.16 ...

  9. python3 完全理解赋值,浅copy,深copy 通过地址详细理解~

    额...老规矩,先来一天NLP再说,也没几条了. 十,在任何一个系统里,最灵活的部分是最能影响大局的部分 灵活便是有一个以上的选择,选择便是能力,因此最灵活的人便是最有能力的人. 灵活来自减少只相信自 ...

  10. 一键解决更改计算机名后无法启动MSSQLSERVER服务问题

    问题版本:SQL Server 2012. 解决办法:打开服务,Win + R运行services.msc,找到 SQL SERVER(MSSQLSERVER)服务右键->属性,切换至登录选项卡 ...