LambdaFormsInC#–ExpressionVsStatementBodies
// ✅ 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
0 B
|👍
/👎
0 B
|👍
/👎