#1,190 – A Lambda Expression Can Be Recursive
September 25, 2014 1 Comment
A lambda expression can be recursive. That is, it can invoke the same delegate that the lambda is being assigned to. As with any recursive method, you need to make sure that there is termination logic to prevent the recursion from continuing indefinitely.
Below is an example of a simple recursive lambda expression.
// Must assign delegate so that we can // reference it in lambda Action<int> countdown = null; countdown = (i) => { if (i > 0) { Console.WriteLine(i); countdown(i-1); } }; countdown(5);
Pingback: Dew Drop – September 25, 2014 (#1863) | Morning Dew