[Solved] How can I use lambdas with methods in C#?


you can use an expression bodied method.

static double Stirling(long n) => Math.Sqrt((2 * n + 1.0 / 3.0) * Math.PI) * Math.Pow(n, n) / Math.Exp(n);

This is only possible when the method executes a single statement, and in this case, we get rid of the return statement.

or you can use a Func delegate to represent a behaviour as such:

Func<long, double> func = (long n) => Math.Sqrt((2 * n + 1.0 / 3.0) * Math.PI) * Math.Pow(n, n) / Math.Exp(n);

and then call it like so:

double result = func(2);

it’s important to note that the => operator is used for both expression bodied methods (first example above) and lambdas (second example above) but they’re completely different beasts.

solved How can I use lambdas with methods in C#?