为什么C#编译器为lambda生成一个类?

时间:2017-04-17 20:07:04

标签: c# .net lambda

我写了下面的代码:

delegate void MyDelegate();

class Program
{
    static void Main()
    {
        MyDelegate md = () => { };
        md();
    }
}

我希望C#编译器为lambda生成一个静态方法但是当我使用" .Net Reflector" 来查看编译到的代码时,我注意到了一个类( <> c )生成。

using _09;
using System;
using System.Runtime.CompilerServices;
[Serializable, CompilerGenerated]
private sealed class <>c
{
    public static readonly Program.<>c <>9 = new Program.<>c();
    public static MyDelegate <>9__0_0;

    internal void <Main>b__0_0()
    {
    }
}

为什么编译器生成了一个类而不是一个方法?

&#34; .Net Reflector&#34; 中的

&#34;主&#34; 方法:

private static void Main()
{
    MyDelegate delegate2 = (<>c.<>9__0_0 != null) ? <>c.<>9__0_0 : (<>c.<>9__0_0 = new MyDelegate(this.<Main>b__0_0));
    delegate2();
}

如何在静态方法中声明this

0 个答案:

没有答案