码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2020-11-23 12:24:00      阅读:17      评论:0      收藏:0      [点我收藏+]

标签:span   adl   length   idt   +=   ima   forms   ongl   src   

一、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 }
技术图片

  运行结果如下:

  技术图片

 

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

标签:span   adl   length   idt   +=   ima   forms   ongl   src   

原文地址:https://www.cnblogs.com/bruce1992/p/14003128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!