开发者

If we can assign a Lambda expression to a delegate type .Does Lambda expression converted to a delegate internally

Does the lambda expression converted to a delegate of type SqureDelegate in the below code? Are all lambda expression or statments internally a delegate.

public delegate int SqureDelegate(int a);

class Program
{
    static int Squre(int i)
    {            
        return i*i;
    }
    static void Main(string[] args)
    {
        开发者_StackOverflow中文版SqureDelegate ss = x => { return x * x; };
    }
}


I compiled the code, then decompiled with ildasm and this is what happens:

SquareDelegate ss = x => {  return x * x; };

compiles to

IL_0009:  ldftn      int32 MiscTestApp.Program::'<Main>b__0'(int32)
IL_000f:  newobj     instance void MiscTestApp.SquareDelegate::.ctor(object,
                                                                     native int)

and

.method private hidebysig static int32 
      '<Main>b__0'(int32 x) cil managed
{
  // square and return x
}

and

.class public auto ansi sealed MiscTestApp.SquareDelegate
   extends [mscorlib]System.MulticastDelegate
{
   // delegate implementation
}

Basically, .Net compiles SquareDelegate to a class which extends System.MulticastDelegate, then each time a delegate is created, it creates an instance of the SquareDelegate class (IL_0009...) using the temporary method b__0. The compiler generates the private static method b__0 to represent the lambda expression. In this way, each lambda will be converted into a corresponding private method during compilation. Since (to my knowledge) you can't ever use a raw lambda expression (it's always cast to an Action, Func, or other delegate type), all lambda expressions are internally delegates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜