I understand a lambda expression is in essence an inline delegate declaration to prevent the extra step
example
delegate int Square(int x) public class Program { static void Main(String[] args) { Square s = x=>x*x; int result = s(5); Console.WriteLine(result); // gives 25 } }
How does one apply Lambda expressions to multi parameters Something like
delegate int Add(int a, int b) static void Main(String[] args) { // Lambda expression goes here }
How can multi parameters be expressed using Lambda expressions?
A lambda function can have any number of parameters, but the function body can only contain one expression.
The lambda expressions are easy and contain three parts like parameters (method arguments), arrow operator (->) and expressions (method body).
In particular, a lambda function has the following characteristics: It can only contain expressions and can't include statements in its body.
Since a lambda function can only provide the implementation for 1 method it is mandatory for the functional interface to have ONLY one abstract method.
You must understand the Func behavior, where the last parameter is always the output or result
Func<1, 2, outPut>
Func<int, int, int> Add = (x, y) => x + y; Func<int, int, int> diff = (x, y) => x - y; Func<int, int, int> multi = (x, y) => x * y;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With