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)
}
}
Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.
C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).
Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.
You just need to express the specific types. For example:
Func<Func<int, int>, Func<int, int>>
represents a function which takes a (function converting an int to a second int) and returns a (function converting an int to a second int). Or to take two functions and return a third:
Func<Func<int, int>, Func<int, int>, Func<int, int>>
For example:
Func<Func<int, int>, Func<int, int>> applyTwice = (f => x => f(f(x));
This can be returned generically by a method:
public static Func<Func<T,T>, Func<T,T>> ApplyTwice<T>()
{
return func => x => func(func(x));
}
If you want to sum two functions, you might do:
public static Func<int, int> Sum(Func<int, int> first, Func<int, int> second)
{
return x => first(x) + second(x);
}
Now to apply it:
Func<int, int> doubler = x => x * 2;
Func<int, int> squarer = x => x * x;
Func<int, int> doublePlusSquare = Sum(doubler, squarer);
Console.WriteLine(doublePlusSquare(5)); // Prints 35
(Untested, but should be okay...)
If you don't have C# 3 and .NET 3.5 available to you, then declare the following delegates:
public delegate TResult Func<TResult>();
public delegate TResult Func<T, TResult>(T arg);
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
(There's more on my C# Versions page.)
Then you'll need to use anonymous methods, e.g.
public static Func<int, int> Sum(Func<int, int> first, Func<int, int> second)
{
return delegate(int x) { return first(x) + second(x); };
}
Func<int, int> doubler = delegate (int x) { return x * 2; };
Func<int, int> squarer = delegate (int x) { return x * x; };
Func<int, int> doublePlusSquare = Sum(doubler, squarer);
Console.WriteLine(doublePlusSquare(5)); // Prints 35
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With