C#中的' =>'语法是什么意思?

106

我在这个论坛的一些问题中遇到了这个语法,但 Google 和任何其他搜索引擎都会屏蔽除字母和数字以外的所有内容,因此无法搜索出 "=>"。

那么,有人能告诉我它是什么以及如何使用吗?


可能是C# Lambda表达式,为什么要使用它?的重复问题。 - nawfal
7个回答

101

这是lambda操作符。

从C# 3到C# 5,它仅用于Lambda表达式。这基本上是C# 2中引入的匿名方法的一种缩写形式,但也可以转换为表达式树

举个例子:

Func<Person, string> nameProjection = p => p.Name;

等价于:

Func<Person, string> nameProjection = delegate (Person p) { return p.Name; };
在这两种情况下,你都使用一个带有Person参数的委托,返回该人的姓名(作为字符串)。
在C# 6中,相同的语法用于表达式主体成员,例如。
// Expression-bodied property
public int IsValid => name != null && id != -1;

// Expression-bodied method
public int GetHashCode() => id.GetHashCode();

另请参阅:

(事实上有许多类似的问题-请尝试使用lambdalambda-expressions标签。)


27

这是一种更为简洁的方法符号表示法。以下两种写法大致等价:

// explicit method
int MyFunc(int x) {
   return x;
}

// anonymous (name-less) method
// note that the method is "wrapped" up in a hidden object (Delegate) this way
// so there is a very tiny bit of overhead compared to an explicit method
// (though it's really the assignment that causes that and would also happen
// if you assigned an explicit method to a reference)
Func<int, int> MyFunc = 
   delegate (int x) { return x; };

// lambda expression (also anonymous)
// basically identical to anonymous method,
// except with everything inferred as much as possible, intended to be minimally verbose
Func<int, int> MyFunc =
   x => x;

// and => is now also used for "expression-bodied" methods
// which let you omit the return keyword and braces if you can evaluate
// to something in one line
int MyFunc(int x) =>
   x;

将lambda表达式视为“给定某物,返回某物”的方式。在上面的示例中,lambda表达式x => x表示“给定x,返回x”,尽管lambda表达式不一定需要返回某些东西,在这种情况下,您可以将它们视为“给定x,对x做一些事情”。

还要注意,有三种称为“委托”的东西,这可能会非常令人困惑。

匿名方法使用delegate关键字,但定义了一个没有名称的方法:

Func<int, int> = delegate (int x) { return x; };

将方法(匿名、显式或 Lambda)分配给引用会创建一个隐藏的 Delegate 包装对象,使方法可以被引用。(基本上是一种“托管函数指针”。)

另外,您还可以使用 delegate 关键字声明 命名方法签名

public delegate int TestFunc(int x, int y);

TestFunc myFunc = delegate (int x, int y) { return x + y; };

这声明了一个名为TestFunc的签名,它接受两个int并返回一个int,然后声明了一个该类型的委托引用,然后将其分配给具有匹配签名的匿名方法。


这比被接受的答案好多了 - 你解释并区分了术语得非常清楚! - Lou

14

它意味着非常棒。例如:

x => x + 1

表示一个以x作为参数并返回它的后继元素的方法。

button.Click += new EventHandler((sender, e) => methodInfo.Invoke(null, new object[] { sender, e }));

通过调用一个MethodInfo持有的方法,为按钮分配事件处理程序。


11

这里是来自 MSDN 的一个简单示例。

delegate int del(int i);
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25

在“=>”之前的是输入参数,之后的是表达式。您可以有多个输入参数。Lambda主要与Linq一起使用。


9

不要使用像这样的匿名方法:

somevar.Find(delegate(int n)
{
   if(n < 10)
      return n;
});

你只需要像这样写它:
somevar.Find(n => n < 10);

它将根据返回值确定数据类型。

1

这是lambda表达式语法的一部分。Lambda表达式本质上是一个缩写形式的委托或匿名方法。例如,假设我有一个匹配字母表中字母的字符串数组,我可以使用以下LINQ表达式选择包含大于“E”的值的数组成员:

var someLetters = alphabet.Where(l => l > "E");

lambda表达式中“=>”左侧部分标识测试的变量名称(设置为alphabet的各个成员),而“=>”右侧部分标识处理过程。在这种情况下,处理过程产生一个布尔值,Where逻辑使用该值确定是否将字母表的每个成员传递到someLetters数组中。


1

基本上它的意思是“进入”,就像一个参数

MyObjectReference => MyObjectReference.DoSomething()

通常你会使用它们将函数作为参数传递给方法,或在LINQ语句中使用

MyCollection.Where(myobj => myobj.Age>10)

例如。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接