Why does CLR create new class for anonymous method?

后端 未结 1 718
一向
一向 2021-01-15 02:59

I am using anonymous functions in my projects no less. And till know I was thinking that, C# compiler generates just a method using the code used for the anonymous method

1条回答
  •  爱一瞬间的悲伤
    2021-01-15 03:35

    If there is nothing to capture it C# compiler will create private method in the class, if you have variables in closure - inner class will be created.

    Covered in details in Chapter 12 - Delegates and Lambda Expressions

    int local = 42;
    ...Where(value => {return true;})... // private method
    ...Where(value => { return value == local;})... // class 
    

    0 讨论(0)
提交回复
热议问题