给出以下代码:
public class C
{
public void M()
{
var x = 5;
Action<int> action = y => Console.WriteLine(y);
}
}
Run Code Online (Sandbox Code Playgroud)
使用VS2013,.NET 4.5.查看反编译代码时,我们可以看到编译器在调用站点缓存委托:
public class C
{
[CompilerGenerated]
private static Action<int> CS$<>9__CachedAnonymousMethodDelegate1;
public void M()
{
if (C.CS$<>9__CachedAnonymousMethodDelegate1 == null)
{
C.CS$<>9__CachedAnonymousMethodDelegate1 = new Action<int>(C.<M>b__0);
}
Action<int> arg_1D_0 = C.CS$<>9__CachedAnonymousMethodDelegate1;
}
[CompilerGenerated]
private static void <M>b__0(int y)
{
Console.WriteLine(y);
}
}
Run Code Online (Sandbox Code Playgroud)
查看在Roslyn中反编译的相同代码(使用TryRoslyn),产生以下输出:
public class C
{
[CompilerGenerated]
private sealed class <>c__DisplayClass0
{
public static readonly …Run Code Online (Sandbox Code Playgroud)