C# - Anonymouse Methods
All tutorials created in C# to be posted in here.
1 post
Page 1 of 1
First of all its not made by me, its made by Íkay Íknur...
Anonymous Methods and Behind the Scenes
Anonymous Methods provides us to define unnamed methods and most common usage of Anonymous Methods is delegates.
For an example:
Now, we open this application in Reflector(By clicking the open button or drag and drop the file to the treeview).
![Image]()
Now you see that the compiler has generated a completely different method and delegate named as
![Image]()
If we put this event handler assignment into Loaded event of an WPF Window,
we can see generated IL Code like below:
![Image]()
Simply, in this method first, the 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 in 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 was made in.
Lets demonstrate this scenario:
Now, lets take a look at the new class in the project we just made and open it in Reflector again:
![Image]()
In this scenario, the compiler generates a new type named
![Image]()
This is what we see in Window_Loaded Method in IL Codes:
![Image]()
In this method firstly, the compiler generates a new
And finally, the compiler creates a delegate by giving the method which locates in
Thanks for reading this ;) Hope you learnt something of this cooll;
Anonymous Methods and Behind the Scenes
Anonymous Methods provides us to define unnamed methods and most common usage of Anonymous Methods is delegates.
For an example:
Code: Select all
Simply, this statements adds an event handler to the button's click event. But what is really going on behind the scenes? Does this compiler make some extra translations?testButton.Click += {_sender, _args) =>
{
MessageBox.Show("This is a Anonymous Function...");
};
Now, we open this application in Reflector(By clicking the open button or drag and drop the file to the treeview).

Now you see that the compiler has generated a completely different method and delegate named as
Code: Select all
and <WindowLoaded>b__0
Code: Select all
This method includes anonymous methods inline statement.CS$<>9_CachedAnonymousMethodDelegate1

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, the 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 in 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 was made in.
Lets demonstrate this scenario:
Code: Select all
In this method, the Anonymous Method uses local variables which are defined in method that event handler assignment was made in.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)));
};
}
Now, lets take a look at the new class in the project we just made and open it in Reflector again:

In this scenario, the compiler generates a new type named
Code: Select all
This type includes 2 more fields which we used in Anonymous Method declaration and also it includes a method declaration that performs action that we specified in Anonymous Method declaration as we discussed in the previous section.<>c__DisplayClass1

This is what we see in Window_Loaded Method in IL Codes:

In this method firstly, the compiler generates a new
Code: Select all
object then assigns the field values.<>c__DisplayClass1
And finally, the compiler creates a delegate by giving the method which locates in
Code: Select all
type and adds this delegate to the 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 the used object.<>c__DisplayClass1
Thanks for reading this ;) Hope you learnt something of this cooll;
1 post
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023