Anonymous Methods - Behind The Scenes
Introduction
I think one of the most mysterious thing in software development world is compilers. Let's talk about C# compiler specifically.
C# compiler was written in C++ for historical reasons. C# compiler takes source files then generates IL code. But we don't know what's going on inside C# compiler when generating IL code. In this process C# compiler also creates different kind of new structures.
Anonymous Methods - Behind The Scenes
Anonymous methods provide us to define unnamed methods and most common usage of anonymous methods is delegates.
For Example,
btnLambda.Click += (_sender, _args) =>
{
MessageBox.Show("This is an anonymous function");
};
Simply, this statement adds an event handler to the button's click event. But what's going on behind the scenes ? Does compiler make extra translations ?
Firstly, we open this application with reflector.
We see that compiler has generated a different method and delegate named as <WindowLoaded>b__0 and CS$<>9__CachedAnonymousMethodDelegate1. This method includes anonymous method's inline statement.
If we put this event handler assignment into Loaded event of an WPF Window, we can see generated IL Code like below.
Simply, in this method first, compiler creates a delegate which takes generated static method as parameter and then adds this delegate as Button's click event handler.
Using Local Variables in Anonymous Methods
I think using local variables in anonymous methods is the most important feature of anonymous methods. If we add a delegate to an event as event handler explicitly, we can't use any local variables inside this method which assignment operation made in.
Let's demonstrate this scenario
private void Window_Loaded(object sender, RoutedEventArgs e)
{
int x = 10;
double y = 198.30;
btnLambda.Click += (_sender, _args) =>
{
MessageBox.Show(String.Format("This is an anonymous function
result={0}",(x*y)));
};
}
In this method, anonymous method uses local variables which are defined in method that event handler assignment made in.
Now let's look at the new class view of the project with reflector again.
In this scenario, compiler generates a new type named <>c__DisplayClass1. This type includes 2 fields which we used in anonymous method declaration and also includes a method declaration that performs actions we specified in anonymous method declaration as we discussed in previous section.
Window_Loaded Method in IL Codes
In this method firstly, compiler generates new <>c__DisplayClass1 object then assigns field values. And finally, compiler creates a delegate by giving method which locates in <>c__DisplayClass1 type and adds this delegate to button's Click event as event handler. If we use a local object reference in an anonymous method, scenario will be the same. Compiler-generated type will include relevant fields in order to access used object.
Conclusion
As you see, compilers are really mysterious. When programming languages provide some new features, compiler deals with some extra translations and makes developer's life easier.
Maybe that's why I love compilers and programming languages. :)
Hope this helps !
发表评论
o6RnBD Thank you for your blog article.Much thanks again. Fantastic.