C#: Recursive functions with Lambdas

前端 未结 4 687
死守一世寂寞
死守一世寂寞 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:10

    This particular style of function is not supported by C# as a single line declaration. You have to separate out the declaration and definition into 2 lines

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

提交回复
热议问题