Delegates - a 15 minutes quick start tutorial

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

Download source - 3.97 Kb

FeaturesDelegate invoke instance method with parameter. Delegate invoke static method with parameter. Delegate multi-casting: delegate invoking more than one methods at once. InstructionsJust compile and run the sample. The code is documented and is self-explanatory.

Concept/backgroundDelegate 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 fragmentsHere's your delegateDelegate - 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 WorkerClasspublic 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!

ConclusionThat's it. It's simple right?

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航