//委托方法
public static int Add(int a, int b) { return a + b; }
//委托方法的定义
delegate int AddDelegate(int a, int b);
//委托初始化(方法名)
AddDelegate addDelegate = new AddDelegate(Add);
//委托的调用(方法参数)
int c = AddDelegate.Invoke(3, 4);
代码
using System; using System.Collections; using System.Collections.Generic; namespace ConsoleApp2 { class Program { class Class1 { static void Main(string[] args) { AddDelegate AddDelegate = new AddDelegate(Add); int c = AddDelegate.Invoke(3, 4); Console.WriteLine(c); } public static int Add(int a, int b) { int c = a + b; return c; } } delegate int AddDelegate(int a, int b); } }
解决代码重用,写出通用委托
//泛型委托方法
public static void WXKINT(int a ,int b) { int c = a + b; Console.WriteLine(c); } public static void WXKDOUBLE(double a, double b) { double c = a + b; Console.WriteLine(c); }
//泛型委托的定义
delegate void WXKDelegate(T a, T b);
//委托初始化(方法名)
WXKDelegatewXKDelegate = new WXKDelegate (WXKINT);
//委托的调用(方法参数)
wXKDelegate(2, 5);//调运委托可以不加Invoke
代码
using System; using System.Collections; using System.Collections.Generic; namespace ConsoleApp2 { class Program { class Class1 { static void Main(string[] args) { WXKDelegatewXKDelegate = new WXKDelegate (WXKINT); wXKDelegate(2, 5);//调运委托可以不加Invoke } public static void WXKINT(int a ,int b) { int c = a + b; Console.WriteLine(c); } public static void WXKDOUBLE(double a, double b) { double c = a + b; Console.WriteLine(c); } } delegate void WXKDelegate (T a, T b); } }
不需要定义委托
//委托方法
public static void Show(string a ,int num) { Console.WriteLine(a); } public static void SHOW2(int a,int b,int c) { Console.WriteLine(c); } public static string SHOW3() { return "有返回值"; } public static int SHOW4(int a) { return a; }
//委托不需定义
//委托初始化和调用(方法名)
//无返回值 Actionaction = new Action (Show); action("无返回值",1); Action action2 = new Action (SHOW2); action2(1,2,3); //有返回值 Func func1 = new Func (SHOW3); Console.WriteLine(func1()); Func func2 = new Func (SHOW4); Console.WriteLine(func2(111));
代码
using System; using System.Collections; using System.Collections.Generic; namespace ConsoleApp2 { class Program { class Class1 { static void Main(string[] args) { //无返回值 Actionaction = new Action (Show); action("无返回值",1); Action action2 = new Action (SHOW2); action2(1,2,3); //有返回值 Func func1 = new Func (SHOW3); Console.WriteLine(func1()); Func func2 = new Func (SHOW4); Console.WriteLine(func2(111)); Console.ReadKey(); } public static string SHOW3() { return "有返回值"; } public static int SHOW4(int a) { return a; } public static void Show(string a ,int num) { Console.WriteLine(a); } public static void SHOW2(int a,int b,int c) { Console.WriteLine(c); } } } }
本质是匿名方法 ,让我们更快更简单完成委托,还可以让实例化委托的方法访问局部变量
起初代码是这样
//最初是这种复杂分开写 Actionaction = new Action (Show); action("我是泛型委托Action"); //还要在下面写委托方法 public static void Show(string a) { Console.WriteLine(a); }
第一步演变
//使用匿名方法 Actionaction2 = new Action (delegate (string msg) { Console.WriteLine(msg); }); action2("我是委托action,使用了匿名方法");
接下来更简单一下
//=>念成gose to Actionaction3 = new Action ( (string msg)=> { Console.WriteLine(msg); }); action3("我是委托action,使用了匿名方法");
更简单的写下来
//一个参数 一个语句的方法 Actionaction4 = new Action ((msg) => Console.WriteLine(msg)); action4("我是委托action,使用了匿名方法");
接下来就是lambda
//一个参数 一个语句的方法 Action action5 = new Action(() => Console.WriteLine("无返回值,无参数")); action5();
//有返回值,单条语句 Funcfunc6 = new Func (() => "有返回值,单条语句"); func6();
//有返回值,多条语句 Funcfunc7 = new Func (() => { Console.WriteLine("有返回值,多条语句"); Console.WriteLine("有返回值,多条语句"); Console.WriteLine("有返回值,多条语句"); Console.WriteLine("有返回值,多条语句"); return "有返回值,多条语句"; }); func7();
代码:
using System; using System.Collections; using System.Collections.Generic; namespace ConsoleApp2 { class Program { class Class1 { static void Main(string[] args) { //最初是这种复杂分开写 Actionaction = new Action (Show); action("我是泛型委托Action"); //使用匿名方法 Action action2 = new Action (delegate (string msg) { Console.WriteLine(msg); }); action2("我是委托action,使用了匿名方法"); //=>念成gose to Action action3 = new Action ( (string msg)=> { Console.WriteLine(msg); }); action3("我是委托action,使用了匿名方法"); //一个参数 一个语句的方法 Action action4 = new Action ((msg) => Console.WriteLine(msg)); action4("我是委托action,使用了匿名方法"); //无返回值 无参数 //一个参数 一个语句的方法 Action action5 = new Action(() => Console.WriteLine("无返回值,无参数")); action5(); //有返回值,单条语句 Func func6 = new Func (() => "有返回值,单条语句"); func6(); //有返回值,多条语句 Func func7 = new Func (() => { Console.WriteLine("有返回值,多条语句"); Console.WriteLine("有返回值,多条语句"); Console.WriteLine("有返回值,多条语句"); Console.WriteLine("有返回值,多条语句"); return "有返回值,多条语句"; }); func7(); } public static void Show(string a) { Console.WriteLine(a); } } } }