What Are Anonymous Methods?

Anonymous methods are a new feature introduced in C # 2.0. It allows developers to declare their function code inline without using a delegate function. Anonymous methods usually require a temporary method. Very few; 2. The code for this method is short, and may even be used when the method declaration is shorter.

Anonymous method

In C # 1.1, declaring and using delegates requires you to have a delegate and an executable method with a matching signature when the delegate is triggered, and an assignment statement that associates a named method with the delegate. As a new feature of C # 2.0, anonymous methods can basically provide the same functionality as previously named methods, but it no longer requires a method that was explicitly created before being associated with a delegate.
You can think of C # anonymous methods as a convenient way to implement the function of association with delegates. If you look at the IL results achieved by anonymous method implementations and named method implementations at the same time, you will find that the difference between the two is very small. when
Create anonymous methods using the delegate keyword, as follows:
button1.Click + = delegate (System.Object o, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show ("Click!");
};

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?