RosettaCodeData/Task/Higher-order-functions/C-sharp/higher-order-functions-3.cs
2019-09-12 10:33:56 -07:00

24 lines
833 B
C#

using System;
class Program
{
static int Call(Func<int, int, int> f, int a, int b)
{
return f(a, b);
}
static void Main()
{
int a = 6;
int b = 2;
// No lengthy delegate keyword.
Console.WriteLine("f=Add, f({0}, {1}) = {2}", a, b, Call((int x, int y) => { return x + y; }, a, b));
// Parameter types can be inferred.
Console.WriteLine("f=Mul, f({0}, {1}) = {2}", a, b, Call((x, y) => { return x * y; }, a, b));
// Expression lambdas are even shorter (and are most idiomatic).
Console.WriteLine("f=Div, f({0}, {1}) = {2}", a, b, Call((x, y) => x / y, a, b));
}
}