Declaring a delegate type
suggest changeThe following syntax creates a delegate type with name NumberInOutDelegate, representing a method which takes an int and returns an int.
public delegate int NumberInOutDelegate(int input);
This can be used as follows:
public static class Program
{
static void Main()
{
NumberInOutDelegate square = MathDelegates.Square;
int answer1 = square(4);
Console.WriteLine(answer1); // Will output 16
NumberInOutDelegate cube = MathDelegates.Cube;
int answer2 = cube(4);
Console.WriteLine(answer2); // Will output 64
}
}
public static class MathDelegates
{
static int Square (int x)
{
return x*x;
}
static int Cube (int x)
{
return x*x*x;
}
}
A delegate instance such as square is invoked in the same way as the Square method. The caller invokes the delegate, and the delegate calls the target method. This indirection decouples the caller from the target method.
You can declare a generic delegate type, and in that case you may specify that the type is covariant (out) or contravariant (in) in some of the type arguments. For example:
public delegate TTo Converter<in TFrom, out TTo>(TFrom input);
Like other generic types, generic delegate types can have constraints, such as where TFrom : struct, IConvertible where TTo : new().
Avoid co- and contravariance for delegate types that are meant to be used for multicast delegates, such as event handler types. This is because concatenation (+) can fail when variance makes the run-time type differ from the compile-time type. For example, avoid:
public delegate void EventHandler<in TEventArgs>(object sender, TEventArgs e);
Instead, use an invariant generic type:
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
Also supported are delegates where some parameters are modified by ref or out, as in:
public delegate bool TryParser<T>(string input, out T result);
For example, TryParser<decimal> example = decimal.TryParse;. Delegates can also give the last parameter the params modifier. Delegate types can have optional parameters (supply default values). Delegate types can use pointer types like int* or char* in their signatures or return types (use unsafe keyword). A delegate type and its parameters can carry custom attributes.