Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Delegates - a 15 minutes quick start tutorial

0.00/5 (No votes)
9 Aug 2003 1  
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.
    public delegate int SomeDelegate(int nID, string sName);

Methods referenced by SomeDelegate can be found in WorkerClass

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(...)

//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