06-06-2013, 04:23 PM
Delegate And Events
Delegate.pptx (Size: 60.33 KB / Downloads: 13)
Delegate
Normal meaning of delegation is to “depute” or “Entrust” someone on behalf of someone
So in C#, it means, method acting for another mothod
Delegate is an object which contains reference to one or more methods
Steps
Creating and using delegates involves four steps
Delegation Declaration(System.Delegate–base class)
Delegation Method Definition
Delegation Instantiation
Delegate Invocation
Delegation Declaration
Syntax:
modifier delegate return-type delegate_name(parameters);
Although, syntax is similar is similar to method definition (without method body)
The Delegate keywords tell the compiler, that this method is using System.Delegate as base class
Delegate may be defined in:
Inside a class
Outside all classes
As top level object in namespace
Delegation Methods
The method whose reference are encapsulated into a delegate instance are known as Delegation Methods or Callback Entities
Note that Signature and return type of method must match to Delegate signature and return type
Multicast Delegate
We have seen, so far delegate can invoke only one method
It is possible to hold and invoke multiple methods by delegate
Such delegate are called multicast delegate
It is also known as combinational delegates,
But it must satisfy following conditions
The return type of delegates must be void
No parameter of delegate can use out keyword