public static void Show() { Student student = new Student();//声明类以及调用 student.Id = 11; student.Name = ""; student.Study(); NoReturnWithPara method = new NoReturnWithPara(Plus); //2委托的实例化,传入一个方法名称 要求方法签名一致 NoReturnWithPara method1 = Plus; //简明声明委托 //用Lamdba表达式实现 { NoReturnWithPara method2 = (x, y) => Console.WriteLine("x={0},y={1}", x, y); //匿名方法体 } method.Invoke(1, 2); //3调用 method.BeginInvoke(1, 2, null, null); //没有委托就没有异步 method(1, 2); //调用 Plus(4, 5); method += Plus; method += Plus; method += Plus; method -= Plus;//多播委托 //foreach (NoReturnNoPara item in method.GetInvocationList())//获取委托的实列 //{ // item.Invoke(); //} }
public void Show() { int k = 1; { NoReturnNoPara method = new NoReturnNoPara(this.DoNothing); } { //anonymous methods NoReturnNoPara method = new NoReturnNoPara(delegate() { Console.WriteLine("this is do nothing in anonymous methods"); }); } { //Lambda expression NoReturnNoPara method = new NoReturnNoPara(() => //"=>" is called goes to. { Console.WriteLine("this is do nothing in anonymous methods"); }); } { // Lambda expression is not a delegation, or an instance of a delegation. It's a class in another class, a internal method. NoReturnWithPara method = new NoReturnWithPara((int x, int y) => { }); NoReturnWithPara method1 = new NoReturnWithPara((x, y) => { }); // int could be ignored NoReturnWithPara method2 = (int x, int y) => { }; //left is decleration, right is a lambda expression } }
public void DetegateChangeCode() { //1. 1.0 1.1版本时的写法 NoReturnWithPara method1 = new NoReturnWithPara(this.WithParaNothing); //2.0时的写法 NoReturnWithPara method2 = new NoReturnWithPara(delegate(int x, string s) { Console.WriteLine("带参数的方法{0} - {1}", x, s); }); //3.0时的写法 NoReturnWithPara method3 = new NoReturnWithPara((int x, string s) => { Console.WriteLine("带参数的方法{0} - {1}", x, s); }); NoReturnWithPara method4 = new NoReturnWithPara((x, s) => { Console.WriteLine("带参数的方法{0} - {1}", x, s); }); NoReturnWithPara method5 = new NoReturnWithPara((x, s) => Console.WriteLine("带参数的方法{0} - {1}", x, s)); NoReturnWithPara method6 = ((x, s) => { Console.WriteLine("带参数的方法{0} - {1}", x, s); Console.WriteLine("111"); }); }
public void Show() { int k = 1; { //.NET 1.0, 1.1 NoReturnNoPara method = new NoReturnNoPara(this.DoNothing); } { // 2.0 匿名方法 访问到调用地方的变量 NoReturnNoPara method = new NoReturnNoPara(delegate() { Console.WriteLine("DO Nothing"); Console.WriteLine(k); }); } { // 3.0 lambda 左边是参数列表 goes to 右边是方法体 本质就是一个方法 NoReturnNoPara method = new NoReturnNoPara(() => // goes to { Console.WriteLine("DO Nothing"); Console.WriteLine(k); }); } { NoReturnWithPara method = new NoReturnWithPara((int x, int y) => { Console.WriteLine("DO Nothing"); Console.WriteLine(k); }); } { // 可以省略参数类型 自动推算 要么都声明参数类型,要么都不声明 NoReturnWithPara method = new NoReturnWithPara((x, y) => { Console.WriteLine("DO Nothing"); Console.WriteLine(k); }); } { // 方法体只有一行可以省略大括号 NoReturnWithPara method = (x, y) => { Console.WriteLine("DO Nothing"); }; // lambda 表达式是什么? // 首先不是委托 委托是类型 // 也不是委托的实例 // 只是一个方法 (作用) // 实际上编译后是一个类中类,里面的一个internal 方法, 然后被绑定到静态委托类型字段 } }
public void Show() { int k = 1; { //1.0 1.1 NoReturnNoPara method = new NoReturnNoPara(this.DoNothing); } { //2.0 匿名方法 NoReturnNoPara method = new NoReturnNoPara(delegate() { Console.WriteLine(k);//能直接用到这里的变量 Console.WriteLine("This is DoNothing1"); }); } { //3.0 lambda:左边是参数列表 goes to 右边是方法体 本质就是一个方法 NoReturnNoPara method = new NoReturnNoPara(() =>//goes to { Console.WriteLine("This is DoNothing2"); }); } { NoReturnWithPara method = new NoReturnWithPara((int x, int y) => { Console.WriteLine("This is DoNothing3"); }); } {//可以省略掉参数类型 自动推算的 NoReturnWithPara method = new NoReturnWithPara((x, y) => { Console.WriteLine("This is DoNothing4"); }); } {//方法体只有一行,可以去掉大括号和分号 NoReturnWithPara method = new NoReturnWithPara((x, y) => Console.WriteLine("This is DoNothing5")); } { //可以省略 编译器推算的 NoReturnWithPara method = (x, y) => Console.WriteLine("This is DoNothing6"); }//lambda表达式是个什么呢? //首先不是委托 委托是类型 //然后也不是委托的实例 因为这里是省略new 委托() //只是一个方法(作用) //实际上是一个类中类,里面的一个internal方法,然后被绑定到静态的委托类型字段 { NoReturnWithPara method = new NoReturnWithPara((x, y) => { Console.WriteLine("This is DoNothing4"); }); method += new NoReturnWithPara((x, y) => { Console.WriteLine("This is DoNothing4"); }); method -= new NoReturnWithPara((x, y) => { Console.WriteLine("This is DoNothing4"); }); method.Invoke(123, 234);//还是执行2遍 lambda的注册是去不掉的 } { Action <int> act = i => { }; } }
public void Show() { //2.实例化委托 NoReturnNoPara delegateNoParaObj = new NoReturnNoPara(this.DoNothing); delegateNoParaObj.Invoke(); NoReturnWithPara delegateWithParaObj = new NoReturnWithPara(this.WithParaNothing); delegateWithParaObj.Invoke(2, "sss"); for (int i = 0; i < 10; i++) { delegateWithParaObj.BeginInvoke(i, i.ToString(), delegate(IAsyncResult t) { Console.WriteLine("当前线程ID:" + System.Threading.Thread.CurrentThread.ManagedThreadId); }, "哈哈哈"); } }
//2. 委托的使用(在show方法中调用) public void Show() { //以“有参无返回值委托”为例,介绍委托的各种用法 //2.1 用法一 { NoReturnWithPara methord = new NoReturnWithPara(this.Test1); methord.Invoke(1, "唐马儒1"); } //2.2 用法二 { NoReturnWithPara methord = this.Test1; methord.Invoke(2, "唐马儒2"); } //2.3 用法三 DotNet 2.0 时代 { NoReturnWithPara methord = new NoReturnWithPara ( delegate(int id, string name) { Console.WriteLine("{0} {1}", id, name); } ); methord.Invoke(3, "唐马儒3"); } //2.4 用法四 DotNet 3.0 时代 { NoReturnWithPara methord = new NoReturnWithPara ( (int id, string name) => { Console.WriteLine("{0} {1}", id, name); } ); methord.Invoke(4, "唐马儒4"); } //2.5 用法五 委托约束 { NoReturnWithPara methord = new NoReturnWithPara ( (id, name) => { Console.WriteLine("{0} {1}", id, name); } ); methord.Invoke(5, "唐马儒5"); } //2.6 用法六 (如果方法体只有一行,可以去掉大括号和分号) { NoReturnWithPara methord = new NoReturnWithPara((id, name) => Console.WriteLine("{0} {1}", id, name)); methord.Invoke(6, "唐马儒6"); } //2.7 用法七 { NoReturnWithPara methord = (id, name) => Console.WriteLine("{0} {1}", id, name); methord.Invoke(7, "唐马儒7"); methord(7, "唐马儒7"); } //2.8 用法八 { //Func<int, bool> methord = (x) => // { // return x > 6; // }; //等价于(原理,当只有一个参数的时候,可以省略参数的小括号,当方法体只有一行的时候,可以省略大括号和分号,即lambda形式) Func <int, bool> methord = x => x > 6; //等价于 bool methord(int x) => x > 6; Console.WriteLine(methord(8)); } }
public void Show() { string nameOut = "糯米飞机"; { Student student = new Student(); student.Id = 1; student.Name = "打兔子的猎人"; student.Age = 27; student.ClassId = 2; student.Study(); } { Student student = new Student() { Id = 1, Name = "打兔子的猎人", Age = 27, ClassId = 2 }; student.Study(); } { NoReturnWithPara method = new NoReturnWithPara(this.DoNothing); //2 委托的实例化 method.Invoke(1, "Gain"); //3 委托的调用 } { NoReturnWithPara method = this.DoNothing; method.Invoke(2, "憨豆"); } { NoReturnWithPara method = new NoReturnWithPara( delegate(int id, string name) { Console.WriteLine(nameOut); Console.WriteLine("{0} {1} DoNothing"); }); method.Invoke(3, "yoyo"); } { NoReturnWithPara method = new NoReturnWithPara( (int id, string name) => //goes to //lambda表达式:是一个匿名方法,是一个方法 { Console.WriteLine(nameOut); Console.WriteLine("{0} {1} DoNothing"); }); method.Invoke(4, "Nine"); } { NoReturnWithPara method = new NoReturnWithPara( (id, name) => //去掉参数类型,原因是委托的约束 { Console.WriteLine("{0} {1} DoNothing"); }); method.Invoke(5, "小昶"); } { //如果方法体只有一行,那么可以去掉大括号和分号,多行不行 //如果参数只有一个 可以去掉小括号 //如果没有参数,直接用小括号 NoReturnWithPara method = new NoReturnWithPara((id, name) => Console.WriteLine("{0} {1} DoNothing", id, name)); method.Invoke(6, "梦醒时分"); } { NoReturnWithPara method = (id, name) => Console.WriteLine("{0} {1} DoNothing", id, name); method.Invoke(7, "小石头"); method(8, "Y"); } { //0到16个参数 无返回值的委托 Action act1 = () => Console.WriteLine("1234"); Action <int> act2 = t => { }; Action <int, NoReturnWithPara, LambdaShow, LambdaShow, string, string, string, string, string, string, string, string, string, string, string, string> act3 = null; Action <int, NoReturnWithPara, NoReturnWithPara, LambdaShow, LambdaShow, string, string, string, string, string, string, string, string, string, string, string, string> act4 = null; } { //0到16个参数 带返回值的委托 Func <int> func1 = () => 1; int i = func1(); Func <int, string> func2 = t => "1234"; Func <int, int, NoReturnWithPara, LambdaShow, LambdaShow, string, string, string, string, string, string, string, string, string, string, string, string> func3 = null; } { //多返回值:1 返回对象 2 ref out WithReturnWithParaRefOut method = new WithReturnWithParaRefOut( delegate(ref DateTime time, out int i) { i = 1; return(this); }); //lambda表达式不行 } }
public static void Show() { NoReturnWithPara method = new NoReturnWithPara(Study);//委托实例化,要求传入的方法必须满足委托的约束 method.Invoke("jack", DateTime.Now); NoReturnWithPara method1 = new NoReturnWithPara( delegate(string name, DateTime now) { Console.WriteLine("{0}{1}正在学习Lambda表达式", name, now); }); //使用匿名方法,把方法名去掉 method1.Invoke("jack", DateTime.Now); NoReturnWithPara method2 = new NoReturnWithPara( (string name, DateTime now) => { Console.WriteLine("{0}{1}正在学习Lambda表达式", name, now); }); //Lmabda表达式 把delegate换成了=> method2.Invoke("jack", DateTime.Now); NoReturnWithPara method3 = new NoReturnWithPara( (name, now) => { Console.WriteLine("{0}{1}正在学习Lambda表达式", name, now); }); //去掉了参数类型,委托实例化要求传入的方法必须满足委托的约束 method3.Invoke("jack", DateTime.Now); NoReturnWithPara method4 = (name, now) => { Console.WriteLine("{0}{1}正在学习Lambda表达式", name, now); }; //去掉了new NoReturnWithPara(); method4.Invoke("jack", DateTime.Now); NoReturnWithPara method5 = (name, now) => Console.WriteLine("{0}{1}正在学习Lambda表达式", name, now); //去掉了{},条件是方法体只有一行; method5.Invoke("jack", DateTime.Now); NoReturnWithPara method10 = (s, b) => Console.WriteLine("{0}{1}正在学习Lambda表达式", s, b); method10.Invoke("jack", DateTime.Now); Console.WriteLine("==============================================="); WithReturnWithoPara func = (x, y) => { return(x * y); }; WithReturnWithoPara func1 = (x, y) => x * y;//去掉了{},条件是方法体只有一行,带返回值的话,return就去掉; func.Invoke(3, 4); func1.Invoke(3, 4); Console.WriteLine("==============================================="); NoReturnNoPara action = () => { }; NoReturnNoPara action1 = () => { Console.WriteLine("12345"); }; Console.WriteLine("==============================================="); WithReturnNoPara fun = () => "erfdfa"; Console.WriteLine("==============================================="); //系统自带的各种委托 #region 带返回值的委托 Action act1 = () => { }; //泛型委托 Action <string> act2 = name => { Console.WriteLine(); };//一个参数的话,可以去掉小括号 //最多可以有16个参数 Action <string, int, long, DateTime, char, double, decimal> act3 = (par1, par2, par3, par4, par5, par6, par7) => { }; #endregion #region 带返回值的委托 Func <string> fun1 = () => "fdafds"; Func <DateTime> fun2 = () => DateTime.Now; //最多可以有16个参数,带一个返回值 Func <string, int, long, DateTime, char, double, decimal, DateTime> fun3 = (par1, par2, par3, par4, par5, par6, par7) => DateTime.Now; Func <string, int, long, DateTime, char, double, decimal, Action> fun4 = (par1, par2, par3, par4, par5, par6, par7) => act1; #endregion }
public void Show() { { NoReturnWithPara func = (x, y) => { }; func += (x, y) => Console.WriteLine("123");; func.Invoke(3, 4); } //System.MulticastDelegate Student student = new Student() { Id = 96, Name = "Summer" }; student.Study(); //Predicate<int> //Func<string> { NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);//2 委托的实例化 method.Invoke();//3 委托的调用 method(); //method.BeginInvoke(null, null); this.DoNothing(); } { NoReturnNoPara method = new NoReturnNoPara(this.DoNothing); //当前类的实例方法 NoReturnNoPara method1 = new NoReturnNoPara(MyDelegate.DoNothingStatic); //当前类的静态方法 NoReturnNoPara method2 = new NoReturnNoPara(student.Study); //其他类的实例方法 NoReturnNoPara method3 = new NoReturnNoPara(Student.StudyAdvanced); //其他类的静态方法 } { NoReturnNoPara method = new NoReturnNoPara(this.DoNothing); method += new NoReturnNoPara(this.DoNothing); method += new NoReturnNoPara(MyDelegate.DoNothingStatic); method += new NoReturnNoPara(student.Study); method += new NoReturnNoPara(Student.StudyAdvanced); method += new NoReturnNoPara(new Student().Study); method += new NoReturnNoPara(Student.StudyAdvanced); method += new NoReturnNoPara(() => Console.WriteLine("这里是lambda表达式")); //+=就是把多个方法按顺序排成列表,invoke时按顺序执行 //method.Invoke(); method -= new NoReturnNoPara(this.DoNothing); method -= new NoReturnNoPara(MyDelegate.DoNothingStatic); method -= new NoReturnNoPara(student.Study); method -= new NoReturnNoPara(new Student().Study); method -= new NoReturnNoPara(Student.StudyAdvanced); method -= new NoReturnNoPara(Student.StudyAdvanced); method -= new NoReturnNoPara(() => Console.WriteLine("这里是lambda表达式")); //-=就是从这个实例上,从后往前挨个匹配,找到第一个完全吻合的移除掉,且只移除一个,找不到不异常 method.Invoke(); //method.BeginInvoke(null, null); foreach (NoReturnNoPara item in method.GetInvocationList()) { item.BeginInvoke(null, null); } } }
public void Show() { { //1.0最普通的写法 NoReturnWithPara mathod = new NoReturnWithPara(this.TestFunction); //实例化委托 mathod.Invoke(123, "hzq"); //委托实例的调用 } { //2.0方法,匿名方法 NoReturnWithPara mathod = new NoReturnWithPara( delegate(int id, string name) { Console.WriteLine($"ID:{id},Name:{name}"); } ); mathod.Invoke(123, "hzq"); } { //3.0 Lambda表达式 NoReturnWithPara mathod = new NoReturnWithPara( (int id, string name) => //gose to Lambda表达式,左边是参数列表右边是方法体 { Console.WriteLine($"ID:{id},Name:{name}"); } ); mathod.Invoke(123, "hzq"); } { //Lambda表达式的语法糖,省略类型 NoReturnWithPara mathod = new NoReturnWithPara( (id, name) => { //编译器语法糖 Console.WriteLine($"ID:{id},Name:{name}"); } ); mathod.Invoke(333, "cc"); } { //Lambda表达式的进一步缩减 NoReturnWithPara mathod = (id, name) => Console.WriteLine($"ID:{id},Nmae:{name}"); mathod.Invoke(30789, "hzq"); } //.Net3.0系统自带的委托,Action,Func Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Lambda表达式(系统自带委托,无参数无返回值)"); { //1.0 Action act = new Action(this.TestNoParaFuntion); act.Invoke(); } { //2.0 Action act = new Action( delegate() { this.TestNoParaFuntion(); } ); act.Invoke(); } { Action act = new Action( () => this.TestNoParaFuntion() ); act.Invoke(); } { Action act = () => this.TestNoParaFuntion(); act.Invoke(); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Lambda表达式(系统自带委托,有参数无返回值)"); { //2.0匿名函数 Action <int, string> act = new Action <int, string>( //泛型委托 delegate(int id, string name) { Console.WriteLine($"ID:{id},Name:{name}"); } ); act.Invoke(6666, "HZQ"); } { //3.0写法,lambda表达式 Action <int, string> act = (id, name) => Console.WriteLine($"ID:{id},Nmae:{name}"); act.Invoke(567, "HZQ"); } }
public void Show() { Console.WriteLine("lambda演变历史"); { Console.WriteLine(".NetFramework1.0 1.1"); Console.WriteLine("执行无参无返回值委托"); NoReturnNoPara method = new NoReturnNoPara(this.DoNothing); method.Invoke(); } { Console.WriteLine("执行有参无返回值委托"); NoReturnWithPara method = new NoReturnWithPara(this.Study); method.Invoke(20200316, "2020年3月16日17:56:21"); } { Console.WriteLine(".NetFramework2.0 匿名方法,delegate关键字"); //可以访问局部变量 NoReturnWithPara method = new NoReturnWithPara(delegate(int id, string name) { Console.WriteLine($"***这是方法***:匿名方法:{id} {name}"); }); method.Invoke(20200316, "2020年3月16日17:02:23"); } { Console.WriteLine(".NetFramework3.0 把delegate关键字去掉,增加了一个箭头goes to 也就是:=>"); NoReturnWithPara method = new NoReturnWithPara( (int id, string name) => { Console.WriteLine($"***这是方法***:{id} {name}"); }); method.Invoke(20200316, "2020年3月16日17:06:31"); Console.WriteLine("Action方法"); Action <string, string> actionA = (string str1, string str2) => { Console.WriteLine($"***这是方法***:参数1:{str1},参数2:{str2}"); }; actionA.Invoke("ABC", "CBA"); } { Console.WriteLine("//省略参数类型,编译器的语法糖,虽然没写,编译时还是有的,根据委托推算"); NoReturnWithPara method = new NoReturnWithPara( (id, name) => { Console.WriteLine($"{id} {name}"); }); method.Invoke(20200316, "2020年3月16日17:13:11"); Console.WriteLine("Action方法"); Action <int, string> actionB = (int1, str1) => { Console.WriteLine($"***这是方法***:参数1{str1},参数2:{str1}"); }; actionB.Invoke(20200316, "2020年3月16日17:20:03"); } { Console.WriteLine("如果方法体只有一行,可以去掉大括号和分号"); NoReturnWithPara method = new NoReturnWithPara( (id, name) => Console.WriteLine($"{id} {name}")); method.Invoke(20200316, "2020年3月16日17:22:59"); } { Console.WriteLine("new NoReturnWithPara可以省掉,也是语法糖,编译器自动加上"); NoReturnWithPara method = (id, name) => Console.WriteLine($"{id} {name}"); method.Invoke(123, "大涛"); } Console.WriteLine("lambda表达是什么? lambda只是实力化委托的一个参数,就是个方法"); Console.WriteLine("lambda是匿名方法,但是编译的时候会分配一个名字,还会产生一个私有sealed类,这里增加一个方法"); { Console.WriteLine("lambda在多播委托"); NoReturnWithPara method = new NoReturnWithPara(this.Study); method += this.Study; method += (id, name) => Console.WriteLine($"{id} {name}"); method -= this.Study; method -= (id, name) => Console.WriteLine($"{id} {name}"); Console.WriteLine("多播委托里面的lambda无法移除, 不是2个实例,其实是2个不同的方法,因为在编译的时候,会生成两个方法名不同的方法"); method.Invoke(20200316, "2020年3月16日17:26:12"); } { Console.WriteLine("==========有返回值,有参数=========="); Func <int, int, int> funcA = (a, b) => { return(a + b); }; Console.WriteLine("如果方法体只有一行也可以省略大括号"); Func <int, int, int> funcB = (a, b) => a + b; int c = funcA.Invoke(12, 13); int d = funcB.Invoke(10, 30); Console.WriteLine($"FuncA返回值:{c}"); Console.WriteLine($"FuncB返回值:{d}"); } }
public void Show() { //lambda演变历史 { //.NetFramework1.0 1.1 NoReturnNoPara method = new NoReturnNoPara(this.DoNothing); } int i = 10; { NoReturnWithPara method = new NoReturnWithPara(this.Study); method.Invoke(123, "董小姐"); } { //.NetFramework2.0 匿名方法,delegate关键字 //可以访问局部变量 NoReturnWithPara method = new NoReturnWithPara(delegate(int id, string name) { Console.WriteLine(i); Console.WriteLine($"{id} {name} 学习.Net高级班"); }); method.Invoke(234, "Harley"); } { //.NetFramework3.0 把delegate关键字去掉,增加了一个箭头goes to //lambda表达式 参数列表=>方法体 NoReturnWithPara method = new NoReturnWithPara( (int id, string name) => { Console.WriteLine($"{id} {name} 学习.Net高级班"); }); method.Invoke(123, "Coder"); } { NoReturnWithPara method = new NoReturnWithPara( (id, name) =>//省略参数类型,编译器的语法糖,虽然没写,编译时还是有的,根据委托推算 { Console.WriteLine($"{id} {name} 学习.Net高级班"); }); method.Invoke(123, "加菲猫"); } { NoReturnWithPara method = new NoReturnWithPara( (id, name) => Console.WriteLine($"{id} {name} 学习.Net高级班")); //如果方法体只有一行,可以去掉大括号和分号 method.Invoke(123, "ZJ"); } { NoReturnWithPara method = (id, name) => Console.WriteLine($"{id} {name} 学习.Net高级班"); method.Invoke(123, "大涛"); //new NoReturnWithPara可以省掉,也是语法糖,编译器自动加上 } //lambda表达是什么? lambda只是实力化委托的一个参数,就是个方法 //lambda是匿名方法,但是编译的时候会分配一个名字 //还会产生一个私有sealed类,这里增加一个方法 { //lambda在多播委托 NoReturnWithPara method = new NoReturnWithPara(this.Study); method += this.Study; method += (id, name) => Console.WriteLine($"{id} {name} 学习.Net高级班1"); method -= this.Study; method -= (id, name) => Console.WriteLine($"{id} {name} 学习.Net高级班1"); //多播委托里面的lambda无法移除, 不是2个实例,其实是2个不同的方法 method.Invoke(345, "北纬23"); } { //匿名方法或者lamdba表达式是不是只能是无返回值的 Action action0 = () => { }; Action <string> action1 = s => Console.WriteLine(s); //参数只有一个 可以省略小括号 Func <int> func0 = () => DateTime.Now.Month; //如果方法体只有一行,去掉大括号分号return int iResult = func0.Invoke(); } }