Get the App
SLTechnology News&Howtos  ›  Development  › 

How to associate multiple methods in C #

Shulou Source: shulou.com Published: 2022-06-02 09:05:42 11月03日 Update

This article mainly shows you "how to relate multiple methods in C#". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "how to relate multiple methods in C#".

The C # language is still quite common. Here we mainly introduce the association of several methods in C #, including an anonymous method and a naming method associated with it.

C # Association of multiple methods

C # Association of multiple methods is the same as naming methods, it is possible to associate multiple anonymous methods with the same delegate. This can be useful in many cases-the first thing that comes to mind is to add a simple handler to the click event of a button. The following code shows a delegate with both an anonymous method and a named method associated with it:

PrivatedelegatevoidExample4 (stringfirstName,stringlastName); privatevoidbtnExample4_Click (objectsender,EventArgse) {/ / Setupourparameters. Stringparameter1= "Zach"; stringparameter2= "Smith"; / / CreateaninstanceoftheExample4delegatewithan / / anonymousmethod Example4example= newExample4 (delegate (stringfirstName,stringlastName) {MessageBox.Show ("Example4:" + firstName+ "" + lastName);}); / / Addanothermethodtothedelegate-thistime / / anamedmethod. Example+=newExample4 (Example4NamedMethod); / / Executethedelegate. Example (parameter1,parameter2);} privatevoidExample4NamedMethod (stringfirstName,stringlastName) {MessageBox.Show ("Example4Method:" + firstName+ "" + lastName);} above is all the content of the article "how to relate multiple methods in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Methods associations multiple clips content articles delegates learning help things events code at the same time common situations buttons aspects easy to understand more useful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology Xiaomi NVidia macOS