C#基础知识---Lambda表达式

一、Lambda表达式简介

Lambda表达式可以理解为匿名函数,可以包含表达式和语句。它提供了一种便利的形式来创建委托。

Lambda表达式使用这个运算符--- “=>”,它读成“goes to” ,该运算符的左边为输入参数,右边是表达式或者语句块。

 

二、例子

  例1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lambda
{
class Program
{
static void Main(string[] args)
{

//下面是C# 1中创建委托实例的代码
Func<string, int> del1 = new Func<string, int>(CallBackMethod);

//C#2中用匿名方法来创建委托实例,CallBackMethod
Func<string, int> del2 = delegate (string text)
{
return text.Length;
};

//C# 3中使用Lambda表达式来创建委托实例
Func<string, int> del3 = (string text) => text.Length;

// 可以省略参数类型string,把上面代码再简化为:
Func<string, int> del4 = (text) => text.Length;

// 如果Lambda表达式只需一个参数,并且那个参数可以隐式指定类型时,
// 此时可以把圆括号也省略,简化为:
Func<string, int> del5 = text => text.Length;

int length = del5("Test");
Console.WriteLine("Length:" + length);

Console.ReadKey();
}

private static int CallBackMethod(string str)
{
return str.Length;
}
}
}

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows.Forms;
 7 
 8 namespace Lambda
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14 
15             //下面是C# 1中创建委托实例的代码
16             Func<string, int> del1 = new Func<string, int>(CallBackMethod);
17 
18             //C#2中用匿名方法来创建委托实例,CallBackMethod
19             Func<string, int> del2 = delegate (string text)
20             {
21                 return text.Length;
22             };
23 
24             //C# 3中使用Lambda表达式来创建委托实例
25             Func<string, int> del3 = (string text) => text.Length;
26                                  
27             // 可以省略参数类型string,把上面代码再简化为:
28             Func<string, int> del4 = (text) => text.Length;
29                              
30             // 如果Lambda表达式只需一个参数,并且那个参数可以隐式指定类型时,
31             // 此时可以把圆括号也省略,简化为:
32             Func<string, int> del5 = text => text.Length;
33 
34             int length = del5("Test");
35             Console.WriteLine("Length:" + length);
36 
37             Console.ReadKey();
38         }
39 
40         private static int CallBackMethod(string str)
41         {
42             return str.Length;
43         }
44     }
45 }
复制代码

  例2: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lambda
{
class Program
{
static void Main(string[] args)
{
Button button1 = new Button() { Text = "按钮1", Width = 50, Height = 50, Name = "button1" };
button1.Left = 10;
button1.BackColor = System.Drawing.Color.Red;

Button button2 = new Button() { Text = "按钮二", Width = 50, Height = 50, Left = 70, Name = "button2" };
button2.BackColor = System.Drawing.Color.Green;

// C# 2中使用匿名方法来订阅事件
button1.Click += delegate (object sender, EventArgs e)
{
ReportEvent("Click事件", sender, e);
};


// C# 3Lambda表达式方式来订阅事件
button2.Click += (sender, e) => ReportEvent("Click事件", sender, e);


Form form = new Form { Name = "在控制台中创建的窗体", AutoSize = true };

form.Controls.Add(button1);
form.Controls.Add(button2);
// 运行窗体

string str = "";
bool isShow = false;
while (!isShow)
{
str = Console.ReadLine();
switch (str)
{
case "show":
isShow = true;
Application.Run(form);
break;
case "quit":
Environment.Exit(0);
break;

}
}
Console.ReadKey();
}
// 记录事件的回调方法
private static void ReportEvent(string title, object sender, EventArgs e)
{
Console.WriteLine("事件名称:{0}", title);
Console.WriteLine("激发事件的对象:{0}", (sender as Button).Name);
Console.WriteLine("事件参数类型: {0}", e.GetType());
Console.WriteLine();
Console.WriteLine();
}
}
}

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows.Forms;
 7 
 8 namespace Lambda
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             Button button1 = new Button() { Text = "按钮1", Width = 50, Height = 50, Name = "button1" };
15             button1.Left = 10;
16             button1.BackColor = System.Drawing.Color.Red;
17 
18             Button button2 = new Button() { Text = "按钮二", Width = 50, Height = 50, Left = 70, Name = "button2" };
19             button2.BackColor = System.Drawing.Color.Green;
20 
21             // C# 2中使用匿名方法来订阅事件
22             button1.Click += delegate (object sender, EventArgs e)
23             {
24                 ReportEvent("Click事件", sender, e);
25             };
26 
27 
28             // C# 3Lambda表达式方式来订阅事件
29             button2.Click += (sender, e) => ReportEvent("Click事件", sender, e);
30 
31 
32             Form form = new Form { Name = "在控制台中创建的窗体", AutoSize = true };
33 
34             form.Controls.Add(button1);
35             form.Controls.Add(button2);
36             // 运行窗体
37 
38             string str = "";
39             bool isShow = false;
40             while (!isShow)
41             {
42                 str = Console.ReadLine();
43                 switch (str)
44                 {
45                     case "show":
46                         isShow = true;
47                         Application.Run(form);
48                         break;
49                     case "quit":
50                         Environment.Exit(0);
51                         break;
52 
53                 }
54             }
55             Console.ReadKey();
56         }
57         // 记录事件的回调方法
58         private static void ReportEvent(string title, object sender, EventArgs e)
59         {
60             Console.WriteLine("事件名称:{0}", title);
61             Console.WriteLine("激发事件的对象:{0}", (sender as Button).Name);
62             Console.WriteLine("事件参数类型: {0}", e.GetType());
63             Console.WriteLine();
64             Console.WriteLine();
65         }
66     }
67 }
复制代码

  运行结果如下:

  

 
posted @ 2020-11-19 00:37  MaxBruce  阅读(1158)  评论(0编辑  收藏  举报