Friday, September 7, 2012

Recursive lambda expression

void Main()
{
///////////////////////////////////////////////
// BOOK - C# Unleashed 4.0
// Chapter, Page - Chap 18 Events, Pg 859-861

/*
///////////////////////////////////////////////
// PROBLEM - recursive lambda expression
//
Func fac2 = n => n == 0 ? 1 : n * fac2(n - 1); // will not work
fac2(5).Dump();
*/

///////////////////////////////////////////////
// SOLUTION - initialize the lambda expression variable to null before assigning it the actual lambda expression
//
Func fac = null;
fac = n => n == 0 ? 1 : n * fac(n - 1); // will work
Console.WriteLine(fac(5));
}





Action finishedHandler = () => {
Console.Beep();
// Finished has been called: we can unhook both Tick and Finished now.
countDown.Tick -= tickHandler;
countDown.Finished -= finishedHandler;
};

Action finishedHandler; // this variable is unassigned!
Action __temp = () => {
Console.Beep();
// Finished has been called: we can unhook both Tick and Finished now.
countDown.Tick -= tickHandler;
countDown.Finished -= finishedHandler;
};

No comments:

Post a Comment

Followers

Blog Archive