Delegate, Method as Parameter.

Source: Internet
Author: User

  1. An agent that takes a method as a parameter to another method. A function pointer similar to C.
    usingSystem;usingSystem.Windows.Forms;usingSystem.Threading;namespacedemo{ Public Delegate voidGreeting (stringname);  Public classDemo {Private Static voidGreetingenglish (stringname) {Console.WriteLine ("hello,{0}", name); }        Private Static voidGreetingchinese (stringname) {Console.WriteLine ("hello, {0}", name); }        //A delegate that takes a method as a parameter to another method. A delegate can represent a sequence of return values and parameter types that are consistent with their methods.        Private Static voidGreetingpeople (stringname, greeting greet)        {greet (name); }                     Public Static voidMain () {greetingpeople ("Jeffrey Richter", Greetingenglish); Greetingpeople ("Andy Lau", Greetingchinese);        Console.readkey (); }    }}
  2. To bind a method to a delegate
    usingSystem;usingSystem.Threading;namespacedemo{ Public Delegate voidGreeting (stringname);  Public classDemo {Private Static voidGreetingenglish (stringname) {Console.WriteLine ("hello,{0}", name); }        Private Static voidGreetingchinese (stringname) {Console.WriteLine ("hello, {0}", name); }        Private Static voidGreetingfrance (stringnom) {Console.WriteLine ("bonjour,{0}", nom); }        //Delegate:use the Method name as Parameters.        Private Static voidGreetingpeople (stringname, greeting greet)        {greet (name); }                     Public Static voidMain () {greetingpeople ("Jeffrey Richter", Greetingenglish); Greetingpeople ("Andy Lau", Greetingchinese); //bind Method to DelegateGreeting Chinese = Greetingchinese; Greeting 中文版= Greetingenglish; 中文版+ = greetingfrance; Chinese ("Xu Ming will"); 中文版 ("Matt Petrek");        Console.readkey (); }    }}
  3. Delegates and interfaces
    usingSystem;usingSystem.Threading;namespacedemo{Interfaceigreeting{voidGreeting (stringname);    };  Public classgreetingchinese:igreeting { Public voidGreeting (stringname) {Console.WriteLine ("hello, {0}", name); }    }         Public classgreetingenglish:igreeting { Public voidGreeting (stringname) {Console.WriteLine ("hello,{0}", name); }    }     Public classDemo {Private Static void Greeting(stringname, igreeting greeting) {Greeting.        Greeting (name); }         Public Static voidMain () {igreeting Chinese=NewGreetingchinese (); Igreeting 中文版=NewGreetingenglish (); Chinese. Greeting (  " Xu Ming " ); 中文版. Greeting ("Jeffrey Richter" ); Greeting ("Matt Petrik"  , 中文版);        Console.readkey (); }    }}
  4. Better encapsulation, the origin of the event
    usingSystem;usingSystem.Threading;namespacedemo{ Public Delegate void Greet(stringname);  Public classGreetingmanager { PublicGreet Delegreet;  Public voidGreeting (stringname, Greet Greet)        {greet (name); }    }     Public classDemo {Private Static voidGreetingenglish (stringname) {Console.WriteLine ("hello,{0}", name); }        Private Static voidGreetingchinese (stringname) {Console.WriteLine ("hello, {0}", name); }        //Main ()         Public Static voidMain () {greetingmanager gm=NewGreetingmanager (); Gm.delegreet = Greetingchinese; Gm.delegreet + = Greetingenglish; gm. Greeting ("Matt Petrik"  , gm.delegreet);        Console.readkey (); }    }}
  5. Better encapsulation, ready to introduce events
    usingSystem;usingSystem.Threading;namespacedemo{ Public Delegate voidGreet (stringname);  Public classGreetingmanager { PublicGreet Delegreet;  Public voidGreeting (stringname) {            if(delegreet!=NULL) {delegreet (name); }        }    }     Public classDemo {Private Static voidGreetingenglish (stringname) {Console.WriteLine ("hello,{0}", name); }        Private Static voidGreetingchinese (stringname) {Console.WriteLine ("hello, {0}", name); }        //Main ()         Public Static voidMain () {greetingmanager gm=NewGreetingmanager (); Gm.delegreet=Greetingchinese; Gm.delegreet+=Greetingenglish; gm. Greeting ("Matt Petrik");        Console.readkey (); }    }}
  6. The purpose of the delegate is to expose it to the outside to allow the client to register the method. The event guarantee is always private within the class and can be considered a property that is customized for delegate.
    usingSystem;usingSystem.Threading;namespacedemo{ Public Delegate voidGreet (stringname);  Public classGreetingmanager {//Public Greet Delegreet;         Public EventGreet Eventgreet;  Public voidGreeting (stringname) {            if(eventgreet!=NULL) Eventgreet (name); }    }     Public classDemo {Private Static voidGreetingenglish (stringname) {Console.WriteLine ("hello,{0}", name); }        Private Static voidGreetingchinese (stringname) {Console.WriteLine ("hello, {0}", name); }        //Main ()         Public Static voidMain () {greetingmanager gm=NewGreetingmanager (); gm. Eventgreet+=Greetingchinese; gm. Eventgreet+=Greetingenglish; //greeting call from client, that 's not good!gm. Greeting ("Matt Petrik");        Console.readkey (); }    }}
  7. . NET Framework for delegates and events
    Of

Delegate, Method as Parameter.

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.