多个参数的Lambda表达式

我理解lambda表达式本质上是一个内联委托声明,以防止额外的步骤

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 } } 

如何将Lambda表达式应用于多参数

  delegate int Add(int a, int b) static void Main(String[] args) { // Lambda expression goes here } 

如何使用Lambda表达式表示多个参数?

您必须了解Func行为,其中最后一个参数始终是输出结果

Func <1,2,outPut>

 Func Add = (x, y) => x + y; Func diff = (x, y) => x - y; Func multi = (x, y) => x * y; 

是。 如果有其他(零或> 1)lambda参数,请在它们周围使用括号。

例子

 Func add = (a,b) => a + b; int result = add(1, 3); Func constant = () => 42; var life = constant(); 
 delegate int Multiplication(int x, int y) public class Program { static void Main(String[] args) { Multiplication s = (o,p)=>o*p; int result = s(5,2); Console.WriteLine(result); // gives 10 } }