Thursday, December 18, 2008

Whar are delegates in .NET?

Delegate can be said as container or a reference type which encapsulates the address of a function mentioned.

It can be clearly said as when a specific event is required to be raised on button click, check box check changed then frame work calls the specific delegate assigned to the control event and accordingly the function whose address was encapsulated by that delegate will be called


Public delegate delhandler ()

Public void source()
Delhandler objdelhandler;
objdelhandler=new Delhandler(this. DelTest);

Public void DelTest()

So when the function "DelTest" is invoked, then "Hai" is written on console.

Multi Cast Delegates

This can be said as multiple functions getting called from a single delegate invoke call.

This includes the process of combining multiple delegates into one and then dynamically invoking at once.

Public void DelTest1()
Console.WriteLine(“Hai 1”);

Public void DelTest2()
Console.WriteLine(“Hai 2”);

Public Delegate Delhandler ()

Public void source()
Delhandler objdelhandler1;
Delhandler objdelhandler2;
Delegate [] objdelAll;
Objdelhandler1=new Delhandler(this. DelTest1);
Objdelhandler2=new Delhandler(this. DelTest2);

objdelAll=MultiCastDelegate.Combine(Objdelhandler1, Objdelhandler2);

From the above process it is clear that we have two delegates, ofcourse of same type the two delegates encapsulating different function addresses.

So there is a process of invoking 2 delegates with 2 invoke statements each or combing two delegates using multi cast delegate getting into an array and then by dynamic invoke.

No comments:

Post a Comment