System.Func
(System.Action
for functions that do not return anything). For example, the statement
System.Func<double,double> square = delegate(double x){return x*x;}declares a variable
square
that references a function that
takes a double argument and returns a double (in this particular case
the square of its argument). The delegates can be used just as any other
type in a C-sharp program. For example,
Func<double,double> f; f=Sin; double sin1 = f(1); f=Cos; double cos1 = f(1); WriteLine($"sin(1)={sin1} cos(1)={cos1}");
You can pass around delegates just like any other objects in Csharp.
For example, here is a funcion, make_table
, that makes
a table of values of a another function given as the argument to
make_table
,
static void make_table(Func<double,double> f){ for(double x=0;x<10;x+=1)WriteLine($"{x} {f(x)}"); }
One can call make_table
as
make_table(square); make_table(Sin);
make_table
of the "square" function like
this,
make_table( delegate(double x){return x*x;} );There is syntactic sugar for anonymous delegates in C# called "lambda expressions" (like in Python). The previous call to
make_table
with a lambda expression would look like this,
make_table( (double x) => x*x );
double a=0; Func<double> f = delegate(){ return a; }; WriteLine(f()); /* prints 0 */ a=7; WriteLine(f()); /* prints 7 */ a=9; WriteLine(f()); /* prints 9 */
public static Func<double> makefun(double a){ Func<double> fun = delegate(){a++;return a;}; return fun; /* "a" is captured here */ }include '../foot.htm' ?>