Click here to Skip to main content
16,016,783 members
Articles / Programming Languages / C#
Tip/Trick

Factorial Simplified using lambda

Rate me:
Please Sign up or sign in to vote.
4.20/5 (7 votes)
2 May 2011CPOL 18.7K   1   5
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 7:02
Nelson LaQuet10-May-11 7:02 
GeneralNice Work. It is nothing but Creating Delegate Call and cal... Pin
Joshi, Rushikesh5-May-11 0:39
professionalJoshi, Rushikesh5-May-11 0:39 
GeneralRe: ok thanks Pin
Steven.Pinto20005-May-11 1:52
Steven.Pinto20005-May-11 1:52 
Generalnice :) Pin
AmitMangal4-May-11 1:32
AmitMangal4-May-11 1:32 
GeneralRe: Thank you Pin
Steven.Pinto20004-May-11 2:33
Steven.Pinto20004-May-11 2: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.