Naming method (C # programming guide)

Source: Internet
Author: User

C # programming guide Naming method (C # programming guide)The delegate can be associated with the naming method. When a delegate is instantiated using a named method, this method will be passed as a parameter, for example, C # copy the code // declare a delegate: Delegate void del (int x ); // define a named method: void dowork (int K ){/*... * // instantiate the delegate using the method as a parameter: del d = obj. dowork; this is called the naming method. The delegate constructed using the naming method can encapsulate static methods or instance methods. In previous C # versions, naming is the only way to instantiate a delegate. However, when the system overhead for creating a new method is unnecessary, C #2.0 allows you to instantiate the delegate and immediately specify the code block that the delegate will process when called. These are called anonymous methods (C # programming guide ). RemarksThe method passed as the delegate parameter must have the same signature as the delegate declaration. The delegated instance can encapsulate static or instance methods. Although the delegate can use the out parameter, it is not recommended to use it in multi-channel broadcast event delegation because it is unable to determine which delegate to call. Example 1The following is a simple example of declaring and using delegation. Note that the delegate del and associated method multiplynumbers have the same signature C # copy the code // declare a delegatedelegate void del (int I, Double J); Class mathclass {staticvoid main () {mathclass M = new mathclass (); // delegate instantiation using "multiplynumbers" del d = m. multiplynumbers; // invoke the delegate object. system. console. writeline ("invoking the delegate using 'multiplynumbers ':"); For (INT I = 1; I <= 5; I ++) {d (I, 2 );}} // declare the associated method. void multiplynumbers (int m, double N) {system. console. write (M * n + "");}} OutputInvoking the delegate using 'multiplynumbers ': 2 4 6 8 10 Example 2In the following example, a delegate is mapped to both the static method and the instance method, and specific information is returned respectively. C # copy the code // declare a delegatedelegate void del (); Class sampleclass {publicvoid instancemethod () {system. console. writeline ("A message from the instance method. ");} staticpublicvoid staticmethod () {system. console. writeline ("A message from the static method. ") ;}} class testsampleclass {staticvoid main () {sampleclass SC = new sampleclass (); // map the delegate to the instance method: del d = SC. instancemethod; D (); // map to the static method: D = sampleclass. staticmethod; D ();}} OutputA message from the instance method. A message from the static method. (Source: msdn)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.