-
A delegate is a type-safe function pointer.
-
Available in any .NET language.
-
A C# example is:
delegate int Massage(string s);
-
Suppose we also declare:
private static int StringLength(string pString)
{
return pString.Length;
}
-
We can now write:
Massage tMassage = new Massage(StringLength);
The variable tMassage now contains a pointer to the
StringLength method.
-
A method can be written in terms of a parameter
that is a delegate:
private static void iProcess(Massage pMassage)
{
string tString = Console.ReadLine();
int tInt = pMassage(tString);
Console.WriteLine(tInt);
}
-
This contains the call:
int tInt = pMassage(tString);
-
The function
that is to be called can be supplied as an argument
when iProcess is called:
iProcess(tMassage);