Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the anonymous method of C#

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what is the anonymous method of C#". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In C # 1.1, declaring and using a delegate requires you to have a delegate and an executable method with a matching signature when the delegate is triggered, as well as an allocation statement that associates the named method with the delegate. As a new feature of C# 2.0, C# anonymous methods can basically provide the same functionality as previous named methods, but it no longer requires a method that is explicitly created before it is associated with a delegate.

You can think of anonymous methods as a convenient way to associate with delegates. If you look at the IL results achieved by both anonymous and named method implementations, you will find that the difference between the two is very small. When the compiler encounters an anonymous method, it creates a named method in the class and associates it with the delegate. So the performance of anonymous methods is very similar to that of naming methods at run time-- the increase in performance is reflected in the developer's productivity, not in execution at run time.

How to use C# Anonymous methods

It is easier to explain and understand anonymous methods with more code and less preaching. The following example should illustrate how you can take advantage of C# anonymous methods:

Basic knowledge

Using the C# anonymous method is easy. You just need to put the anonymous method in the associated statement that you usually put in the named method. In the following example, I associate anonymous methods with delegates:

# regionSimpleexample-Example1 privatedelegatevoidExample1 (); privatevoidbtnExample1_Click (objectsender,EventArgse) {/ / DeclareaninstanceoftheExample1delegate. / / YoucanseewhereI'musingtheanonymous / / methodinplaceofanamedmethod-itfollows / / thedelegate () keyword. Example1example= newExample1 (delegate () {MessageBox.Show ("Example1");}); example ();} # endregion "what is the anonymous method of C#", thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report