1

I wrote the code below:

delegate void MyDelegate();

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

I expected that C# compiler generates a static method for the lambda but when I used ".Net Reflector" to view the code that compiled to, I noticed that a class (<>c) generated.

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()
    {
    }
}

Why the compiler generated a class instead of a method?

"Main" method in ".Net Reflector":

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();
}

How declared this in a static method?

qazwsx123
  • 161
  • 1
  • 11

0 Answers0