What is Func, how and when is it used

前端 未结 8 1802
悲哀的现实
悲哀的现实 2020-11-27 10:53

What is Func<> and what is it used for?

8条回答
  •  执笔经年
    2020-11-27 11:09

    Func represents a function, that takes (T1, T2, ..., Tn) arguments and returns Tr.

    For example, if you have a function:

    double sqr(double x) { return x * x; }
    

    You could save it as some kind of a function-variable:

    Func f1 = sqr;
    Func f2 = x => x * x;
    

    And then use exactly as you would use sqr:

    f1(2);
    Console.WriteLine(f2(f1(4)));
    

    etc.

    Remember though, that it's a delegate, for more advanced info refer to documentation.

提交回复
热议问题