5
In some cases a function/method contains only one line in its scope, see the illustrative example:
static int Soma(int a, int b)
{
return a + b;
}
Meanwhile, to a new feature in C# 6.0 that allows declaring a function/method in lambda expression on only one line, see in the illustrative example:
static int Subtrai(int a, int b) => a - b;
And the way to call these functions and the same:
var resultadoSoma = Soma(10, 5);
var resultadoSubtracao = Subtrai(10, 9);
WriteLine($"Resultado da soma = {resultadoSoma}\nResultado da subtração = {resultadoSubtracao}");
Exit:
Sum result = 15
Result of subtraction = 1
Dotnetfiddle complete of the example.
Doubts
This raised the following doubts:
- There is some limitation in a function/method in lambda syntax, if yes what?
- This function/method in lambda syntax can be called delegate?
- What is the purpose of this new feature?