wingman007 icon

LambdaFormsInC#–ExpressionVsStatementBodies

wingman007 | PRO | 10/19/25 05:37:18 AM UTC | 0 ⭐ | 14936 👁️ | Never ⏰ | [C#, lambda, function]
C# |

490 B

|

Software

|

0 👍

/

0 👎

// ✅ Named method – complex logic
static int Factorial(int n)
{
    int result = 1;
    for (int i = 2; i <= n; i++)
        result *= i;
    return result;
}
 
// ✅ Named expression-bodied method – simple logic
static int Square(int x) => x * x;
 
// ✅ Anonymous lambda – expression-bodied
Func<int, int> addOne = x => x + 1;
 
// ✅ Anonymous lambda – statement-bodied
Func<int, int> addOneVerbose = x =>
{
    int y = x + 1;
    return y; // must return explicitly
};
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎