一、什么是委托:
委托是寻址方法的.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带返回值的只执行完后,只得到最后一个结果,所以没有意义。
 
委托使用案例:一个学生类,一个学生管理静态类,可以通过委托,实现学生集合的筛选
 public class Student
{
public int Id { get; set; } public string Name { get; set; } public int ClassId { get; set; } public int Age { get; set; }
} public static class StudentManager
{
public static List<Student> students = new List<Student>()
{
new Student(){ Id=,Name="张三",ClassId=,Age= },
new Student(){ Id=,Name="李四",ClassId=,Age= },
new Student(){ Id=,Name="王五",ClassId=,Age= },
new Student(){ Id=,Name="赵六",ClassId=,Age= },
new Student(){ Id=,Name="杨幂",ClassId=,Age= },
new Student(){ Id=,Name="范冰冰",ClassId=,Age= },
new Student(){ Id=,Name="张学友",ClassId=,Age=},
new Student(){ Id=,Name="张三1",ClassId=,Age= },
new Student(){ Id=,Name="张三2",ClassId=,Age= },
new Student(){ Id=,Name="张三3",ClassId=,Age= },
new Student(){ Id=,Name="张三4",ClassId=,Age= },
new Student(){ Id=,Name="张三5",ClassId=,Age= },
new Student(){ Id=,Name="张三6",ClassId=,Age= },
new Student(){ Id=,Name="张三7",ClassId=,Age= },
new Student(){ Id=,Name="张三8",ClassId=,Age= },
new Student(){ Id=,Name="张三9",ClassId=,Age= },
new Student(){ Id=,Name="张三0",ClassId=,Age= },
new Student(){ Id=,Name="张三11",ClassId=,Age= },
new Student(){ Id=,Name="张三a",ClassId=,Age= },
new Student(){ Id=,Name="张三b",ClassId=,Age= },
new Student(){ Id=,Name="张三c",ClassId=,Age= },
new Student(){ Id=,Name="张三d",ClassId=,Age= },
new Student(){ Id=,Name="张三e",ClassId=,Age= },
new Student(){ Id=,Name="张三f",ClassId=,Age= },
new Student(){ Id=,Name="张三g",ClassId=,Age= },
new Student(){ Id=,Name="张三h",ClassId=,Age= },
new Student(){ Id=,Name="张三i",ClassId=,Age= },
new Student(){ Id=,Name="张三j",ClassId=,Age= },
new Student(){ Id=,Name="张三k",ClassId=,Age= },
}; public static List<Student> FindStudents(Func<Student,bool> func)
{
List<Student> stus = new List<Student>(); foreach (var item in students)
{
if (func(item))
{
stus.Add(item);
}
}
return stus;
} /// <summary>
/// 查找ClassId为3001的学生
/// </summary>
/// <param name="student">学生</param>
/// <returns>是否为3001班级的学生</returns>
public static bool GetClassId(Student student)
{
if (student.ClassId==)
{
return true;
} return false; }
/// <summary>
/// 年龄大于20的学生
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
public static bool GetBigAge(Student student)
{
if (student.Age>)
{
return true;
}
return false;
}
/// <summary>
/// 年龄大于15 并且ClassId为1021
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
public static bool GetStuByClassIdAndAge(Student student)
{
if (student.Age > && student.ClassId==)
{
return true;
}
return false;
} }

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

//List<Student> stus = StudentManager.students;

            //Console.WriteLine("姓名---年龄---班级--编号");
//foreach (var item in stus)
//{
// Console.WriteLine(item.Name+"---"+item.Age+"---"+item.ClassId+"---"+item.Id);
//} List<Student> stus1= StudentManager.FindStudents(StudentManager.GetStuByClassIdAndAge); Console.WriteLine("姓名---年龄---班级--编号");
foreach (var item in stus1)
{
Console.WriteLine(item.Name + "---" + item.Age + "---" + item.ClassId + "---" + item.Id);
}

.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. setContentType与setCharacterEncoding的区别

    setCharacterEncoding只是设置字符的编码方式 setContentType除了可以设置字符的编码方式还能设置文档内容的类型 1.setCharacterEncoding respon ...

  2. MyBatis新手教程(一)

    MyBatis本是apache的一个开源项目iBatis,2010年这个项目由apache 迁移到了 google,并改名为MyBatis,2013年迁移到Github. MyBatis是一个优秀的持 ...

  3. EffictiveC++笔记 第1章

    Chapter 一: 条款 1 :视 C++为一个语言联邦 (P41 ) c++其实可以视为有四个部分: C Object-Oriented C++ Template C++ STL 条款 2:尽量以 ...

  4. 利用css+原生js制作简易钟表

    利用css+原生js制作简单的钟表.效果如下所示 实现该效果,分三大块:html.javascript.css html部分html部分比较简单,定义一个clock的div,内部有原点.时分秒针.日期 ...

  5. JAVA线程及简单同步实现的原理解析

    线程 一.内容简介: 本文主要讲述计算机中有关线程的相关内容,以及JAVA中关于线程的基础知识点,为以后的深入学习做铺垫.如果你已经是高手了,那么这篇文章并不适合你. 二.随笔正文: 1.计算机系统组 ...

  6. Docker 容器

    1.  容器 在过去,如果要开始编写Python应用程序,首先要做的就是在机器上安装Python运行时环境.但是,这就造成了这样一种情况:你的机器上的环境需要完美,以便你的应用程序能够按预期运行,而且 ...

  7. c#命名规范汇总12条

    前言 在刚学习c#的时候,在脑子根本就么有命名规范这个概念,有了一定入门的基础,也很难严格要求自己去规范代码的命名,工作后,发现自己的命名和其他人的命名总会有一些出入,总会闹出一些尴尬的笑话,这里汇总 ...

  8. 『备注』GDI+ 绘制文本有锯齿,透明背景文本绘制

    背景: GDI+ 绘制文本 时,如果 背景是透明的 —— 则会出现 锯齿. //其实,我不用这三个 属性 好多年了 //而且,这三个属性 在关键时刻还有可能 帮倒忙 //关键是:这三个属性,鸟用都没有 ...

  9. uni-app—从安装到卸载

    uni-app实现了一套代码,同时运行到多个平台.支持iOS模拟器.Android模拟器.H5.微信开发者工具.支付宝小程序Studio.百度开发者工具.字节跳动开发者工具 工具安装 开发uni-ap ...

  10. #if和#ifdef的区别

    学习STM32偶然发现:在Keil中直接预先定义宏USE_STDPERIPH_DRIVER,但是却没有指定宏的值.而在头文件中判断用的是如下代码: #ifdef USE_STDPERIPH_DRIVE ...