设为首页 加入收藏

TOP

.Net 委托 delegate 学习(一)
2019-09-17 17:50:50 】 浏览:47
Tags:.Net 委托 delegate 学习
一、什么是委托:
委托是寻址方法的.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=1,Name="张三",ClassId=1001,Age=15 },
            new Student(){ Id=2,Name="李四",ClassId=1001,Age=15 },
            new Student(){ Id=3,Name="王五",ClassId=1001,Age=15 },
            new Student(){ Id=4,Name="赵六",ClassId=1001,Age=15 },
            new Student(){ Id=5,Name="杨幂",ClassId=1001,Age=14 },
            new Student(){ Id=6,Name="范冰冰",ClassId=101,Age=14 },
            new Student(){ Id=7,Name="张学友",ClassId=1021,Age=14},
            new Student(){ Id=8,Name="张三1",ClassId=1021,Age=16 },
            new Student(){ Id=9,Name="张三2",ClassId=1001,Age=17 },
            new Student(){ Id=10,Name="张三3",ClassId=1001,Age=15 },
            new Student(){ Id=11,Name="张三4",ClassId=1001,Age=19 },
            new Student(){ Id=12,Name="张三5",ClassId=1001,Age=25 },
            new Student(){ Id=13,Name="张三6",ClassId=1003,Age=25 },
            new Student(){ Id=14,Name="张三7",ClassId=1003,Age=25 },
            new Student(){ Id=15,Name="张三8",ClassId=1003,Age=25 },
            new Student(){ Id=16,Name="张三9",ClassId=1003,Age=25 },
            new Student(){ Id=17,Name="张三0",ClassId=1003,Age=25 },
            new Student(){ Id=18,Name="张三11",ClassId=1003,Age=15 },
            new Student(){ Id=19,Name="张三a",ClassId=1011,Age=15 },
            new Student(){ Id=20,Name="张三b",ClassId=1011,Ag
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇asp.net core系列 23 EF模型配置(.. 下一篇Asp.net Core应用程序部署为服务

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目