c# - Simple Delegate (delegate) vs. Multicast delegates -
i have gone through many articles still not clear difference between normal delegates create , multicast delegates.
public delegate void mymethodhandler(object sender); mymethodhandler handler = new mymethodhandler(method1); handler += method2; handler(someobject);
the above delegate mymethodhandler call these 2 methods. multicast delegates come in. have read can call multiple methods afraid basic understanding delegates not correct.
this article explains pretty well:
delegate void del(string s); class testclass { static void hello(string s) { system.console.writeline(" hello, {0}!", s); } static void goodbye(string s) { system.console.writeline(" goodbye, {0}!", s); } static void main() { del a, b, c, d; // create delegate object references // method hello: = hello; // create delegate object b references // method goodbye: b = goodbye; // 2 delegates, , b, composed form c: c = + b; // remove composed delegate, leaving d, // calls method goodbye: d = c - a; system.console.writeline("invoking delegate a:"); a("a"); system.console.writeline("invoking delegate b:"); b("b"); system.console.writeline("invoking delegate c:"); c("c"); system.console.writeline("invoking delegate d:"); d("d"); } } /* output: invoking delegate a: hello, a! invoking delegate b: goodbye, b! invoking delegate c: hello, c! goodbye, c! invoking delegate d: goodbye, d! */
Comments
Post a Comment