A delegate acts on behalf of a method. It is the representative of this method. The delegate below represents all methods that return a Boolean and have a single argument of type Integer:
delegate bool MathDelegate(int x);
To demonstrate this generality, we can create a void method that takes two arguments, the delegate itself and its single argument:
void AboutNumber(MathDelegate Func, int x)
{
if (Func(x)) //Invoke the delegate.
Response.Write(" is ");
else
Response.Write(" is not ");
}
Now AboutNumber() can run all of the following methods:
bool IsEven(int x)
{
if (x % 2 == 0)
return true;
else
return false;
}
bool IsPrime(int x)
{
for(int i = 2; i > (x /2); i++)
if (x % i == 0)
return true;
return false;
}
So, because of delegates a variable number of methods can be funneled through one method.