In C#, how does one create a delegate type that maps delegate types to a delegate type?
In particular, in my example below, I want to declare a delegate Sum
such that (borrowing from mathematical notation) Sum(f,g) = f + g
. I then want to invoke Sum(f,g)
-- such as Sum(f,g)(5)
[this meaning f(5) + g(5)
].
class Example
{
delegate int IntToInt ( int i ) ;
public static int Double ( int i ) { return i * 2 ; }
public static int Square ( int i ) { return i * i ; }
delegate IntToInt IntToIntPair_To_IntToInt ( IntToInt f, IntToInt g ) ;
public static IntToInt Sum ( IntToInt f, IntToInt, g ) { return f + g ; }
public static void Main ( )
{
IntToInt DoubleInstance = Double ;
IntToInt SquareInstance = Square ;
IntToIntPair_To_IntToInt SumInstance = Sum ;
System.Console.WriteLine
( SumInstance ( DoubleInstance, SquareInstance ) ( 5 ) ) ;
// should print 35 = 10 + 25 = Double(5) + Square(5)
}
}