What are delegates?
Let me discuss function pointers first to make delegate a bit easier to understand.
Example of function pointer :-
In above code pt2function is a pointer which can hold an address of a function of class SampleClass and which has same definition as specified in pt2function declaration.
Now delegates are for exactly same purpose as function pointer, only difference is we don't declare any pointers here. Instead .net has implemented it in a completely different way.
Delegates are replacements of function pointers in .Net. Delegate is a kind of class, which mimics declaration of a method that is it supports a return type and arguments Or you can say a named method signature is a delegate.
Syntax (C#) :-
delegate returntype delegate-name(arguements,...n );
Syntax (VB.Net) :-
Delegate [SubFunction] delegate-name(arguements,...n) [as returntype]
Example (VB.Net) :-
Example (C#) :-
As we see here delegate support return type and can have arguments also.
Now lets try to pass a function to delegate. I will use the same delegates that i have provided in example above.
Example VB.Net
Example C#
So its pretty clear from above code that we declare variables of a delegate just like any other class and we pass address of the function as an argument to the constructor.Then we can call the delegate like a function, as done in CallDelegate procedure/function.
In this post I have said that delegate are kind of class. Well, here is some arguement to support my statement. When you declare a delegate, using keyword "delegate", .net compiler treats this syntax in special way. It will create a class with same name as delegate, this class inherits System.MultiCastDelegate class. Although this will be completely hidden from the developer and is performed by the compiler internally.
Example of function pointer :-
int (SampleClass::*pt2function)(float) = NULL;
/* sample class for example*/
class SampleClass{
public: int DoWork(float argument){ return arguement; }
};
pt2function= &SampleClass::DoWork; //assigning function to pointer
SampleClass objInstance; int result3 = (objInstance.*pt2function)(12);
In above code pt2function is a pointer which can hold an address of a function of class SampleClass and which has same definition as specified in pt2function declaration.
Now delegates are for exactly same purpose as function pointer, only difference is we don't declare any pointers here. Instead .net has implemented it in a completely different way.
Delegates are replacements of function pointers in .Net. Delegate is a kind of class, which mimics declaration of a method that is it supports a return type and arguments Or you can say a named method signature is a delegate.
Syntax (C#) :-
delegate returntype delegate-name(arguements,...n
Syntax (VB.Net) :-
Delegate [SubFunction] delegate-name(arguements,...n) [as returntype]
Example (VB.Net) :-
Public Delegate Function Sample(ByVal strArguement As String) As StringExample (C#) :-
delegate void Sample(String strArguement);
As we see here delegate support return type and can have arguments also.
Now lets try to pass a function to delegate. I will use the same delegates that i have provided in example above.
Example VB.Net
Public Class Class1
'declaring an object of Sample delegate
Dim t As Sample = New Sample(AddressOf CoolFunction)
Public Function CoolFunction(ByVal t As String) As String
Return t
End Function
public sub CallDelegate()
t("Raj Kiran Singh") 'or t.Invoke("Raj Kiran Singh")
End sub
End Class
Example C#
public class Class1
{
//declaring an object of Sample delegate
Sample t = new Sample(CoolFunction);
public string CoolFunction(string t)
{
return t;
}
public void CallDelegate()
{
t("Raj Kiran Singh");
//or t.Invoke("Raj Kiran Singh")
}
}
So its pretty clear from above code that we declare variables of a delegate just like any other class and we pass address of the function as an argument to the constructor.Then we can call the delegate like a function, as done in CallDelegate procedure/function.
In this post I have said that delegate are kind of class. Well, here is some arguement to support my statement. When you declare a delegate, using keyword "delegate", .net compiler treats this syntax in special way. It will create a class with same name as delegate, this class inherits System.MultiCastDelegate class. Although this will be completely hidden from the developer and is performed by the compiler internally.
So for this code -
delegate void Sample(String strArguement);Compiler will generate a class named Sample. Sample class will have one constructor and three functions. BeginInvoke, EndInvoke and Invoke.
When delegate has got more than one method attached to it, its called delegate chains. In this case delegate will execute each method and return the value of last method called. Delegate class has got a private variable to store the preceding delegate. This way it forms a list of delegates.


2 Comments:
i have a doubt...without using delegate we call call function so y we use delegate..
let me give you an example if you work with any windows UI in .net, you will find that every control has got events, for example the button control. Now if the button control do not expose any event than how come you are going to put you functionality on the click of the button, considering the fact that button class is allready compiled.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home