What is Func, how and when is it used

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

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

8条回答
  •  攒了一身酷
    2020-11-27 11:00

    Both C# and Java don't have plain functions only member functions (aka methods). And the methods are not first-class citizens. First-class functions allow us to create beautiful and powerful code, as seen in F# or Clojure languages. (For instance, first-class functions can be passed as parameters and can return functions.) Java and C# ameliorate this somewhat with interfaces/delegates.

    Func randInt = (n1, n2) => new Random().Next(n1, n2); 
    

    So, Func is a built-in delegate which brings some functional programming features and helps reduce code verbosity.

提交回复
热议问题