Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

Delegates - a 15 minutes quick start tutorial

Rate me:
Please Sign up or sign in to vote.
3.13/5 (26 votes)
9 Aug 20031 min read 88.4K   1.2K   40   5
Teaches you about delegates using an example program

Features

  • Delegate invoke instance method with parameter.
  • Delegate invoke static method with parameter.
  • Delegate multi-casting: delegate invoking more than one methods at once.

Instructions

Just compile and run the sample. The code is documented and is self-explanatory.

Concept/background

Delegate are just simple mechanism to re-route calls to functions/methods that it references. The idea is simple. Invoking delegate invokes the methods/functions that the delegate references. The question is, why delegate? Why not just invoke the function/method directly?

  • The association between delegate and methods referenced by delegate is established in runtime - so, you get flexibility here.
  • Multi-cast: That's to associate a few methods/functions to ONE delegate. Invoking one delegate fires off ALL methods referenced by that one delegate all at once.

Code fragments

Here's your delegate

  • Delegate - Note that delegate has the same signature to all functions/methods that it references.
  • A delegate can reference more than one function/method - that's called multi-casting.
    C#
    public delegate int SomeDelegate(int nID, string sName);

Methods referenced by SomeDelegate can be found in WorkerClass

C#
public class WorkerClass
{
  //(1) First method (instance method) referenced by delegate:
  public int InstanceMethod(int nID, string sName) {...}
  //(2) Second method (static method) referenced by delegate:
  static public int StaticMethod(int nID, string sName) {...}
};

The only difference between these two methods is that "InstanceMethod" is an instance method, that is, it must be invoked by an actual instance of WorkerClass. StaticMethod is a static member function - invoke it by: WorkerClass.StaticMethod(10,"aaa");

What the function does?

  • It will multiply "nID" by LENGTH of "sName". For example, nID=10, sName="aaa" (Length=3). Therefore return value=10x3=30.
  • It will write to console:
    "InstanceMethod invoked, return value=..." OR
    "StaticMethod invoked, return value=..."

Association between delegates and methods it references can be found in main(...)

C#
//PART 1: invoking instance method            
WorkerClass wr = new WorkerClass();
SomeDelegate d1 = new SomeDelegate(wr.InstanceMethod); 
   //Associating delegate with wr.InstanceMethod

Console.WriteLine("Invoking delegate InstanceMethod, return={0}", 
   d1(5, "aaa") ); //Invoking wr.InstanceMethod with input parameters.

//PART 2: invoking static method
SomeDelegate d2 = new SomeDelegate(WorkerClass.StaticMethod); 
    //Associating delegate with WorkerClass.StaticMethod (NOTE: "wr" 
    //instance is NOT used. The class itself is used!!)
Console.WriteLine("Invoking delegate StaticMethod, return={0}", 
  d2(5, "aaa") ); //Invoking InstanceMethod with input parameters.

//PART 3: MultiCAST!
Console.WriteLine();
Console.WriteLine("Testing delegate multi-cast..");
SomeDelegate d3 = (SomeDelegate) Delegate.Combine(d1, d2);
Console.WriteLine("Invoking delegate(s) d1 AND d2 (multi-cast), return={0} ", 
  d3(5, "aaa") ); //Fire BOTH delegates (d1 AND d2) by firing d3!

Conclusion

That's it. It's simple right?

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Hong Kong Hong Kong
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
SDINES5-Oct-10 1:14
SDINES5-Oct-10 1:14 
clear explanation
GeneralMore details about threading, please! Pin
Uwe Keim11-Aug-03 2:57
sitebuilderUwe Keim11-Aug-03 2:57 
GeneralRe: More details about threading, please! Pin
devvvy11-Aug-03 3:07
devvvy11-Aug-03 3:07 
GeneralRe: More details about threading, please! Pin
Uwe Keim11-Aug-03 3:34
sitebuilderUwe Keim11-Aug-03 3:34 
GeneralRe: More details about threading, please! Pin
J. Dunlap11-Aug-03 9:58
J. Dunlap11-Aug-03 9:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.