where T2: struct
where R: struct;
public class EntryPoint
{
public static double Add( int val1, float val2 ) {
return val1 + val2;
}
static void Main() {
Operation<int, float, double> op =
new Operation<int, float, double>( EntryPoint.Add );
Console.WriteLine( "{0} + {1} = {2}",
1, 3.2, op(1, 3.2f) );
}
}
I’ve declared a generic delegate for an operator method that accepts two parameters and has a
return value. My constraint is that the parameters and the return value all must be value types. For
generic methods, the constraints clauses follow the method declaration but precede the method
body. Notice that at the point of creation in the Main method, I had to tell the compiler the exact
constructed type of the Operation<T1, T2, R> delegate I needed.
Generic ...