Click here to Skip to main content
15,500,453 members
Articles / Programming Languages / C#
Tip/Trick
Posted 2 May 2011

Tagged as

Stats

16.6K views
1 bookmarked

Factorial Simplified using lambda

Rate me:
Please Sign up or sign in to vote.
4.20/5 (7 votes)
2 May 2011CPOL
Factorial Simplified using lambda
Here is a trick which uses recursion in Lambda:

C#
class Factorial
   {
       static void Main()
       {
           //Lambada Expression
           Func<int,int> call = null;
           call = x => x * (x == 1 ? 1 : call(x - 1));
           Console.WriteLine(call(5));
           Console.ReadLine();
       }
   }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Social Group (No members)


Comments and Discussions

 
GeneralThis will generate a compiler warning, as you are referencin... Pin
Nelson LaQuet10-May-11 8:02
MemberNelson LaQuet10-May-11 8:02 
GeneralNice Work. It is nothing but Creating Delegate Call and cal... Pin
Joshi, Rushikesh5-May-11 1:39
professionalJoshi, Rushikesh5-May-11 1:39 
GeneralRe: ok thanks Pin
Steven.Pinto20005-May-11 2:52
MemberSteven.Pinto20005-May-11 2:52 
Generalnice :) Pin
AmitMangal4-May-11 2:32
MemberAmitMangal4-May-11 2:32 
GeneralRe: Thank you Pin
Steven.Pinto20004-May-11 3:33
MemberSteven.Pinto20004-May-11 3:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.