C# 委托和Lambda---基础
分类:
IT文章
•
2025-01-29 18:55:01
【委托】是一个类
可以把一个方法当作另一个方法的参数使用。
声明委托:delegate string 委托名(参数列表);//跟定义方法一样,只是没有方法体,必须使用关键字delegate
使用委托的函数 返回值和参数列表与委托相同【必须记住】
使用委托: 委托名 委托变量名=new 委托(函数名); 委托变量名(参数列表);
1 public ActionResult Index()
2 {
3 de1 de = new de1(EN);
4 string str = de("liuph", 1);//str="liuph is 1 years old"
5 de1 de1 = CN;
6 string str1 = de1("liuph", 1);//str="liuph的年龄是1岁"
7 return View();
8 }
9 delegate string de1(string name, int age);//定义
10 static string CN(string name, int age)
11 {
12 return name + "的年龄是" + age + "岁";
13 }
14 static string EN(string name, int age)
15 {
16 return name + " is " + age + " years old";
17 }
View Code
=========
多播委托
包含多个方法的委托;按顺序调用,返回值必须void(否则返回最后调用方法的值);其中一个发生异常,不再继续向下执行;
1 public ActionResult Index()
2 {
3 de1 de = new de1(CN);//使用
4 de += EN;//连续调用第二个
5 string str = de("liuph", 1);//str="liuph is 1 years old "
6 return View();
7 }
8 delegate string de1(string name, int age);//定义
9 static string CN(string name, int age)
10 {
11 return name + "的年龄是" + age + "岁";
12 }
13 static string EN(string name, int age)
14 {
15 return name + " is " + age + " years old";
16 }
View Code
1 static string str = "";
2 public ActionResult Index()
3 {
4 de1 de = CN;
5 de += EN;
6 de("liuph", 1);
7 //str="liuph的年龄是1岁liuph is 1 years old ";
8 return View();
9 }
10 delegate void de1(string name, int age);
11 static void CN(string name, int age)
12 {
13 str += name + "的年龄是" + age + "岁";
14 }
15 static void EN(string name, int age)
16 {
17 str += name + " is " + age + " years old";
18 }
View Code
发生异常也向下执行 使用GetInvocationList()方法
1 public ActionResult Index()
2 {
3 de1 de = new de1(CN);//使用
4 de += EN;//连续调用第二个
5 Delegate[] des = de.GetInvocationList();
6 string str = "";
7 foreach (de1 item in des)//循环
8 {
9 try
10 {
11 str += item("liu", 1);
12 }
13 catch (Exception)
14 {
15 str += "有错误";
16 }
17 }
18 //EN注释throw 结果 str="liu的年龄是1岁liu is 1 years old "
19 //EN注释return 结果 str="liu的年龄是1岁有错误 "
20 return View();
21 }
22 delegate string de1(string name, int age);
23 static string CN(string name, int age)
24 {
25 return name + "的年龄是" + age + "岁";
26 }
27 static string EN(string name, int age)
28 {
29 throw new Exception("");
30 return name + " is " + age + " years old";
31 }
View Code
==========
匿名方法的委托
1 public ActionResult Index()
2 {
3 de1 de = delegate(string name, int age)
4 {
5 return name + "的年龄是" + age + "岁";
6 };
7 string str = de("liuph", 1);//str="liuph的年龄是1岁";
8 return View();
9 }
10 delegate string de1(string name, int age);
View Code
==========
泛型Action<T>委托表示引用一个void返回类型的方法
1 static string str = "";
2 public ActionResult Index()
3 {
4 Action<int> action;
5 action = de;
6 de(1);
7 //str="liuph的年龄是1岁 ";
8 return View();
9 }
10 public static void de(int i)
11 {
12 str = string.Format("liuph的年龄是{0}岁", i);
13 }
View Code
泛型Func<in T,out TResult>允许调用带返回类型的方法
1 public ActionResult Index()
2 {
3 Func<int,string> action;
4 action = de;
5 string str = de(1);//str="liuph的年龄是1岁 ";
6 return View();
7 }
8 public static string de(int i)
9 {
10 return string.Format("liuph的年龄是{0}岁", i);
11 }
View Code
【Lambda】(个人理解就是匿名委托的简单化)
运算符=>,左边是参数,多个参数用圆括号;右边是实现代码
1 public ActionResult Index()
2 {
3 del myDelegate = x => x * x;
4 int j = myDelegate(5); //j = 25
5 Func<int, string, string> de = (x, y) => qq(x, y);
6 string str = de(12, "你输入的数字是:");//str="你输入的数字是:12"
7
8 Func<string, string, string> de1 = (x, y) => { return x + y; };
9 string str1 = de1("haha", "哈哈");//str1="haha哈哈"
10 return View();
11 }
12 delegate int del(int i);
13 public static string qq(int i, string str)
14 {
15 return str + i;
16 }
View Code
Lambda语句
就是把Lambda表达式写在花括号中
Lambda可以使用外部变量(慎用)
例如:
int y = 12;
del myDelegate = x => x * y;//想获取12*x必须保证使用委托时y值不会变
y = 13;
int j = myDelegate(5); //j = 65