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.
If a function, say make_table_from_0_to_10,
expects a function as an argument, it can declare it using
System.Func, for example,
static void make_table_from_0_to_10(Func<double,double> f){
for(double x=0;x<=10;x+=1)WriteLine($"{x} {f(x)}");
}
One can then call make_table_from_0_to_10 as
make_table_from_0_to_10(square);or
make_table_from_0_to_10(Math.Sin);
double a=0;
Func<double> f = delegate(){ return a; };
a=7;
WriteLine(f()); /* prints 7 */
a=9;
WriteLine(f()); /* prints 9 */
public static Func<double> makefun(){
double a=0;
Func<double> f = delegate(){a++;return a;};
return f; /* "a" is captured here */
}
include '../foot.htm' ?>