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

How to use C# Anonymous method

2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to use the anonymous method of C#. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

C# Anonymous method

This is an extension of the range of variables. However, the following example shows that anonymous parameters can also perform naming methods outside of their code blocks:

PrivatedelegatevoidExample6 (); privateint _ customerId; privatestring _ customerCode; publicint CustomerID {get {return _ customerId;} set {_ customerId = value;}} publicstring CustomerCode {get {return _ customerCode;} set {_ customerCode = value;}} privatevoid btnExample6_Click (object sender, EventArgs e) {/ / Populate out properties. This.CustomerID = 90; this.CustomerCode = "1337HK"; / / Setup the delegate/anonymous method. Example6 example = newExample6 (delegate {this.ShowCustomer (this.CustomerID, this.CustomerCode);}); / / Execute the delegate. Example (); / / Change the properties. This.CustomerID = 54; this.CustomerCode = "L4M3"; / / Execute the delegate again. / / Notice that the new values are reflected. Example ();} privatevoid ShowCustomer (int customerId, string customerCode) {MessageBox.Show (String.Format ("CustomerID: Customer Code:", customerId, customerCode);}

Note that I twice called the delegate associated with the C# anonymous method. You may find it interesting that in these calls, the method outputs two different sets of values. This is because the external variables used in the C# anonymous method are referenced when the C# anonymous method is created. This means that any changes to these variables are reflected when the anonymous function accesses the variable.

You may also notice that there are no parentheses after the delegate keyword in this example. When the C# anonymous method does not need to take parameters, the following parentheses are optional.

This is the end of the article on "how to use the anonymous method of C#". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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: 282

*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