Lambda表达式是C#3.0的一种新语法,语法简洁
为编写匿名方法提供了更简明的函数式的句法.
我通过一个示例来说明Lambda表达式的原理:
Lambda表达式和匿名方法都来源于委托
我们来看看委托的使用
在C#1.0时:
![]()
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5![]()
6
namespace ConsoleApplication3
7![]()
![]()
{
8
public delegate int Calculate(int a, int b);
9
class Program
10![]()
{
11![]()
12
static void Main(string[] args)
13![]()
{
14
int a = 3;
15
int b = 4;
16
Calculate result = new Calculate(Add);
17
Console.WriteLine(result(a,b));
18
Console.Read();
19
}
20![]()
21
public static int Add(int a, int b)
22![]()
{
23
return a + b;
24
}
25
}
26
}
27![]()
C#2.0时可以使用匿名方法:
![]()
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5![]()
6
namespace ConsoleApplication3
7![]()
![]()
{
8
public delegate int Calculate(int a, int b);
9
class Program
10![]()
{
11![]()
12
static void Main(string[] args)
13![]()
{
14
int a = 3;
15
int b = 4;
16![]()
Calculate result = delegate(int ta, int tb)
{ return ta + tb; };
17![]()
18
Console.WriteLine(result(a,b));
19
Console.Read();
20
}
21![]()
22
}
23
}
24![]()
C#3.0使用Lambda表达式:
![]()
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5![]()
6
namespace ConsoleApplication3
7![]()
![]()
{
8
public delegate int Calculate(int a, int b);
9
class Program
10![]()
{
11![]()
12
static void Main(string[] args)
13![]()
{
14
int a = 3;
15
int b = 4;
16
Calculate result = (ta, tb) => ta + tb;
17
Console.WriteLine(result(a,b));
18
Console.Read();
19
}
20![]()
21
}
22
}
23![]()
使用Lambda表达式更简洁,为什么那么简洁.其实是编译器为我们做了很多事情.
Calculate result = (ta, tb) => ta + tb;
这句话 编译器在编译的时候 会为我们生成一个私有的静态方法.透过ILDASM可以看到它是怎么帮助我们的,
编译器为我们声明声明了一个私有静态的Calculate委托字段 和一个静态的私有方法.
通过IL代码可以看看Main方法内部是怎么实现的
![]()
Code
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// 代码大小 58 (0x3a)
.maxstack 3
.locals init ([0] int32 a,
[1] int32 b,
[2] class ConsoleApplication3.Calculate result)
IL_0000: nop
IL_0001: ldc.i4.3
IL_0002: stloc.0
IL_0003: ldc.i4.4
IL_0004: stloc.1
IL_0005: ldsfld class ConsoleApplication3.Calculate ConsoleApplication3.Program::'CS$<>9__CachedAnonymousMethodDelegate1'
IL_000a: brtrue.s IL_001f
IL_000c: ldnull
IL_000d: ldftn int32 ConsoleApplication3.Program::'<Main>b__0'(int32,
int32)
IL_0013: newobj instance void ConsoleApplication3.Calculate::.ctor(object,
native int)
IL_0018: stsfld class ConsoleApplication3.Calculate ConsoleApplication3.Program::'CS$<>9__CachedAnonymousMethodDelegate1'
IL_001d: br.s IL_001f
IL_001f: ldsfld class ConsoleApplication3.Calculate ConsoleApplication3.Program::'CS$<>9__CachedAnonymousMethodDelegate1'
IL_0024: stloc.2
IL_0025: ldloc.2
IL_0026: ldloc.0
IL_0027: ldloc.1
IL_0028: callvirt instance int32 ConsoleApplication3.Calculate::Invoke(int32,
int32)
IL_002d: call void [mscorlib]System.Console::WriteLine(int32)
IL_0032: nop
IL_0033: call int32 [mscorlib]System.Console::Read()
IL_0038: pop
IL_0039: ret
} // end of method Program::Main
可以看出IL_0005: 到IL_001f 这段代码是初始化编译器为我们生成的委托
下面几行IL指令可以看出来是为我们初始化委托
![]()
Code
IL_000c: ldnull //声明一个空的函数指针
IL_000d: ldftn //加载方法指针 指向 '<Main>b__0'(int32,int32) 函数
IL_0013: newobj //实例化委托
IL_0018: stsfld //保存
透过IL代码 我们看到了Lambda表达式的的正真实现方法….
看到了C#语言没有什么改变 只是编译器为我们完成了很多东西
如有不对的.请指出 谢谢!!