Delegate is a type safe function pointer. Delegate point to the function , When you invoke the delegate the function will invoke.
Why don't we call that functio directly?
__________
Becuase the flexiblity we will get in this approach.
1.Syntax of the Delegate it actually syntax of function.A method with delegate keyword as it like class and struct and same as method delegate also have return type. delegate can be use to point to a function which has similar meaning like similar signature .
Syntax :
public delegate void AFunc(string message);
eg: sample function
public void A(string message){
}
How do we make this delegate point to the function
_______________________________
To do We have to create instance of delegate.This is where delegate similar to class.
AFunc del = new AFunc(A) //The constructor of the delegate you have to pass name of the function to which the delegate you want to point to. Now to invoke the function just invoke the delegate.
del("this is a message");
Behind the scene The delegate automatically invoke the function and to that function it will pass the parameter.
IF you want to pass method to a paramter to another method probably we can make use of delegate.This is usefull for frmework developers . You want your class and method to be reusable.You don't want hard code logic.
Point : If you want to pass function to a parameter to another function the deleagte is best option.
0 Comments