C#: Recursive functions with Lambdas

前端 未结 4 686
死守一世寂寞
死守一世寂寞 2020-12-01 04:34

The below does not compile:

Func fac = n => (n <= 1) ? 1 : n * fac(n - 1);

Local variable \'fac\' migh

4条回答
  •  [愿得一人]
    2020-12-01 05:14

    since c# 7.0 you finally can do this in one line using a local function

    int fac(int n) => (n <= 1) ? 1 : n * fac(n - 1);
    

提交回复
热议问题