Saturday, 3 August 2013

Delegates in c#.net

This also called as c++ function pointer.This will maintain address of subprogram.
1.In c++ function pointer is an ordinary statement ,it is no provided with object oriented approach
2.It  can not maintain address of number function
3.It is not type safe,it will not display parameter names and data types of parameters
The solution in .net is delegate concept

What is delegate?
Delegate is a user defined type in .net to maintain address of subprogram.It acts like a function pointer
Delegate can be classified in to 2types
1.Single case Delegate
Delegate maintaining the address of one subprogram is called single cast delagate
Delegate maintaining the address of more than subprogram is called single cast delagae

Implementation of Delegate
It implementation requires three steps
1.Declaring Delegate type
private/public Delegate sub function delagate (param1,..)
Delagate keyword is provided to create Delegate type and it can be subprogram type function type
2.Delegate variable holding subprogram address:
The Delegate variable can maintain address of subprogram ,where sub program type and signature should match with Delegate type
int *p;
float b=20;p=&b-->no accepted
In this case p can hold the address of only integer type variable
3.calling subprogram using Delegate variable