Well I am going through the 'Lambda Expressions' topic (Chapter 17; Delegates, C# Syntactical Sugar for Delegates). Jeffery states that the C# compiler creates a new non-static class in the background that has the following:
- Fields to store all the local variables that have been accessed in the Lambda Expression.
- A method whose body contains the Lambda Expression and whose signature/return type matches the delegate for which the Lambda Expressin has been used.
I have the following two questions:
I did a little debugging myself and saw that if the Lambda Expression modifies the value of a local variable(defined in a method the lambda Expresison is being used in), the new value is reflected outside the expression body too. How is this possible considering the expression is actually in a different class?
Why does the emitted class need to be non-static when the same can be done by Static class perfectly?
I hope this is not a very simple concept that I am unable to comprehend.
Let me know if I need to provide more details.