wingman007

RegularVsLambdas

Sep 25th, 2025 (edited)
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.46 KB | None | 0 0
  1. // Regular method - complex logic
  2. static int Factorial(int n)
  3. {
  4.     int result = 1;
  5.     for (int i = 2; i <= n; i++)
  6.         result *= i;
  7.     return result;
  8. }
  9.  
  10. // Lambda method - simple expression
  11. static int Square(int x) => x * x;
  12.  
  13.  
  14. // Expression-bodied (no explicit return)
  15. Func<int, int> addOne = x => x + 1;
  16.  
  17. // Statement-bodied (explicit return)
  18. Func<int, int> addOneVerbose = x =>
  19. {
  20.     int y = x + 1;
  21.     return y;  // must return explicitly
  22. };
  23.  
Advertisement