Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda expression for multiple parameters

Tags:

c#

linq

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?

like image 743
user544079 Avatar asked Feb 23 '13 03:02

user544079


People also ask

Can a lambda function have multiple parameters?

A lambda function can have any number of parameters, but the function body can only contain one expression.

How many parameters can a lambda expression have?

The lambda expressions are easy and contain three parts like parameters (method arguments), arrow operator (->) and expressions (method body).

Can lambda expressions contain multiple statements?

In particular, a lambda function has the following characteristics: It can only contain expressions and can't include statements in its body.

Can lambda function have multiple methods?

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.


1 Answers

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; 
like image 120
spajce Avatar answered Oct 08 '22 20:10

spajce