C# 多个参数的 Lambda 表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15036584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:51:30  来源:igfitidea点击:

Lambda expression for multiple parameters

c#linq

提问by user544079

I understand a lambda expression is in essence an inline delegate declaration to prevent the extra step

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

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

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

 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?

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

采纳答案by spajce

You must understand the Funcbehavior, where the last parameter is always the outputor result

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

Func<1, 2, outPut>

函数<1, 2, 输出>

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;

回答by Eric J.

Yes. When you have other-than-one (zero, or > 1) lambda arguments, use parenthesis around them.

是的。当您有其他(零或 > 1)个 lambda 参数时,请在它们周围使用括号。

Examples

例子

Func<int, int, int> add = (a,b) => a + b;

int result = add(1, 3);

Func<int> constant = () => 42;

var life = constant();

回答by Aben

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