The below does not compile:
Func<int, int> fac = n => (n <= 1) ? 1 : n * fac(n - 1);
Local variable 'fac' might not be initialized before accessing
How can you make a recursive function with lambdas?
[Update]
Here are also two links that I found interesting to read:
since c# 7.0 you finally can do this in one line using a local function
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
You'll have to create
fac
first und assign it later (which is pretty unfunctional because it depends on multiple assignment) or use so calledY-combinators
.Example:
But note that this might be somewhat hard to read/understand.
Well geez, if you'd just typed "why does a recursive lambda cause a definite assignment error?" into some search engine, you'd have found the answer in my article on the subject.
:-)
http://blogs.msdn.com/ericlippert/archive/2006/08/18/why-does-a-recursive-lambda-cause-a-definite-assignment-error.aspx