Delegates
Delegates
---------------
- a type that enables the storage of references to functions.
Declaration
----------------
- like functions, but without a body, using the 'delegate' keyword.
i.e. public delegate double processDelegate( double a, double b );
- it specifies a function signature consisting of a return type & parameter list.
Usage
---------------
- after the delegation definition, it can be used as a type to declare a variable.
i.e. processDelegate process;
- the delegate variable can then be a reference to any function that has the same signature, ( ie return type & parameters ).
i.e.
static double Multiply( double p1, double p2 )
{
...
}
static double Divide( double p1, double p2 )
{
...
}
process = new processDelegate( Multiply );
// process = new processDelegate( Divide ); <-- alternative
- once this is completed, the assigned function can be called using the delegate variable.
i.e. process( p1, p2 );
PLEASE NOTE
---------------
- different from polymorphism, since only concerened with methods in base & child with same name.
- whereas delegates are to do with any method as long as they have the same signature !!
- the real purpose of delegates is to do with events/event handling.
|