一、什么是委托:
委托是寻址方法的.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. Oracle系列-锁表与解锁解决方案(大招版)-解决问题才是王道

    [Oracle系列-锁表与解锁解决方案(大招版)] --1查看被锁的表 select b.owner,b.object_name,a.session_id,a.locked_mode from v$l ...

  2. html中layui+jfinal模板实现前端搜索功能

    <input type="text" id="campus" class="layui-input" onkeyup="ck ...

  3. 机器学习web服务化实战:一次吐血的服务化之路

    背景 在公司内部,我负责帮助研究院的小伙伴搭建机器学习web服务,研究院的小伙伴提供一个机器学习本地接口,我负责提供一个对外服务的HTTP接口. 说起人工智能和机器学习,python是最擅长的,其以开 ...

  4. “卷积神经网络(Convolutional Neural Network,CNN)”之问

    目录 Q1:CNN 中的全连接层为什么可以看作是使用卷积核遍历整个输入区域的卷积操作? Q2:1×1 的卷积核(filter)怎么理解? Q3:什么是感受野(Receptive field)? Q4: ...

  5. 并发的核心:CAS 与synchronized, Java8是如何优化 CAS 的?

    大家可能都听说说 Java 中的并发包,如果想要读懂 Java 中的并发包,其核心就是要先读懂 CAS 机制,因为 CAS 可以说是并发包的底层实现原理. 今天就带大家读懂 CAS 是如何保证操作的原 ...

  6. python接口自动化(十五)--参数关联接口(详解)

    简介 我们用自动化新建任务之后,要想接着对这个新建任务操作,那就需要用参数关联了,新建任务之后会有一个任务的Jenkins-Crumb,获取到这个Jenkins-Crumb,就可以通过传这个任务Jen ...

  7. 阿里云ecs centos7安装 postgresql 9.4

    rpm -Uvh http://yum.postgresql.org/9.4/redhat/rhel-7-x86_64/pgdg-centos94-9.4-3.noarch.rpm yum insta ...

  8. VSCode Python开发环境配置

    目录 准备工作 VSCode初步 用户界面 快捷键 安装扩展 配置文件与内置终端设置 高级调试配置 小结 参考 博客:blog.shinelee.me | 博客园 | CSDN 准备工作 安装anac ...

  9. Docker最全教程之使用.NET Core推送钉钉消息(十九)

    前言 上一篇我们通过实战分享了使用Go推送钉钉消息,由于技痒,笔者现在也编写了一个.NET Core的Demo,作为简单的对照和说明. 最后,由于精力有限,笔者希望有兴趣的朋友可以分享下使用CoreR ...

  10. 在Unity中实现小地图(Minimap)

    小地图的基本概念众所周知,小地图(或雷达)是用于显示周围环境信息的.首先,小地图是以主角为中心的.其次,小地图上应该用图标来代替真实的人物模型,因为小地图通常很小,玩家可能无法看清真实的模型.大多数小 ...