Anonymous method (C # programming guide)

Source: Internet
Author: User

C # programming guide Anonymous method (C # programming guide)In C # versions earlier than 2.0, the only way to declare a delegate is to use the naming method. C #2.0 introduces the anonymous method. To pass a code block as a delegate parameter, creating an anonymous method is the only method. Example: C # copy the code // create a handler for a click eventbutton1.click + = delegate (system. object o, system. eventargs e) {system. windows. forms. messageBox. show ("Click! ") ;}; Or C # copy the code // create a delegate instancedelegate void del (int x); // instantiate the delegate using an anonymous methoddel d = delegate (int K) {/*... */}; if the anonymous method is used, you do not need to create a separate method. Therefore, the coding system overhead required for instantiating the delegate is reduced. For example, if the system overhead required to create a method is unnecessary, it is very useful to specify a code block at the delegate position. Starting a new thread is a good example. Without creating more methods for the delegate, the thread class can create a thread and contain the code executed by the thread. C # copy the code void startthread () {system. threading. thread T1 = new system. threading. thread (delegate () {system. console. write ("hello,"); system. console. writeline ("world! ") ;}); T1.start () ;}the parameter range of the annotation anonymous method is Anonymous-method-block. It is wrong to use Jump statements (such as Goto, break, or continue) in the anonymous method block outside the block. Use jump statements (such Goto, BreakOr Continue) Is also incorrect. If the range of local variables and parameters includes anonymous method declaration, the local variables and parameters are called external variables of the anonymous method or capture variables. For example, N in the following code segment is an external variable: C # copy the code int n = 0; del d = delegate () {system. console. writeline ("Copy #: {0}", ++ N) ;}; unlike local variables, the life cycle of the external variable continues until the delegate that references the anonymous method meets the garbage collection condition. References to N are captured when the delegate is created. The anonymous method cannot access the ref or out parameters in the external range. In Anonymous-method-blockCannot access any Insecure code. The following example demonstrates two ways to instantiate a delegate: Associate the delegate with an anonymous method. Associate a delegate with a naming method (dowork. Both methods will display a message when calling the delegate. C # copy the code // declare a delegatedelegate void printer (string S); Class testclass {staticvoid main () {// instatiate the delegate type using an anonymous method: printer P = delegate (string J) {system. console. writeline (j) ;}; // results from the anonymous delegate call: P ("the delegate using the anonymous method is called. "); // The delegate instantiation using a named method" dowork ": P = new printer (testclass. dowork); // results from the old style delegate call: P ("the delegate using the named method is called. ");} // The method associated with the named delegate: staticvoid dowork (string K) {system. console. writeline (k) ;}} output the delegate using the anonymous method is called. the delegate using the named method is called. (Source: msdn)

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.