C#operator =>是什么意思?

时间:2022-08-15 16:57:02

Answers to a recent post (Any chances to imitate times() Ruby method in C#?) use the => operator in the usage examples. What does this operator do? I can't locate it in my C# book, and it is hard to search for symbols like this online. (I couldn't find it.)

最近一篇文章的答案(模仿时间的任何机会()C#中的Ruby方法?)在用法示例中使用=>运算符。这个运营商做什么?我无法在我的C#书中找到它,并且很难在网上搜索这样的符号。 (我找不到。)

4 个解决方案

#1


15  

It's not really an operator as such, it's part of the syntax for lambda expressions. In particular => is the bit which separates the parameters from the body of the lambda expression.

它本身并不是一个运算符,它是lambda表达式语法的一部分。特别是=>是将参数与lambda表达式的主体分开的位。

Does your book cover C# 3.0? If not, it won't include lambda expressions. If it does, it should really cover them! Hopefully with the right terminology, you'll be able to find it in the TOC or index.

你的书是否涵盖C#3.0?如果没有,它将不包括lambda表达式。如果确实如此,它应该真正涵盖它们!希望使用正确的术语,您将能够在TOC或索引中找到它。

EDIT: A bit more information: A lambda expression is a piece of syntactic sugar to either create an instance of a delegate or an expression tree (the latter being new to .NET 3.5). Lambda expressions almost entirely replace anonymous methods (from C# 2.0) although they don't support the notion of "I don't care about the parameters" in the way that anonymous methods do.

编辑:更多信息:lambda表达式是一段语法糖,可以创建委托实例或表达式树(后者是.NET 3.5的新手)。 Lambda表达式几乎完全取代了匿名方法(来自C#2.0),尽管它们不像匿名方法那样支持“我不关心参数”的概念。

#2


1  

That will be for a lambda expression:

这将是一个lambda表达式:

http://msdn.microsoft.com/en-us/library/bb397687.aspx

An example is here:

这里有一个例子:

MyControl.OnMouseDown += (sender, e) =>
{
  // Do something in the mouse down event
};

Here I have created a lambda expression event delegate. It basically saves me from having to create a separate function for it in the class.

在这里,我创建了一个lambda表达式事件委托。它基本上使我不必在类中为它创建一个单独的函数。

#3


1  

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

lambda表达式是一个匿名函数,可以包含表达式和语句,可用于创建委托或表达式树类型。

All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block

所有lambda表达式都使用lambda运算符=>,它被读作“转到”。 lambda运算符的左侧指定输入参数(如果有),右侧包含表达式或语句块

http://msdn.microsoft.com/en-us/library/bb397687.aspx

#4


1  

The => token is called the lambda operator.

=>标记称为lambda运算符。

It is used in lambda expressions to separate the input variables on the left side from the lambda body on the right side.

它在lambda表达式中用于将左侧的输入变量与右侧的lambda体分开。

MSDN

#1


15  

It's not really an operator as such, it's part of the syntax for lambda expressions. In particular => is the bit which separates the parameters from the body of the lambda expression.

它本身并不是一个运算符,它是lambda表达式语法的一部分。特别是=>是将参数与lambda表达式的主体分开的位。

Does your book cover C# 3.0? If not, it won't include lambda expressions. If it does, it should really cover them! Hopefully with the right terminology, you'll be able to find it in the TOC or index.

你的书是否涵盖C#3.0?如果没有,它将不包括lambda表达式。如果确实如此,它应该真正涵盖它们!希望使用正确的术语,您将能够在TOC或索引中找到它。

EDIT: A bit more information: A lambda expression is a piece of syntactic sugar to either create an instance of a delegate or an expression tree (the latter being new to .NET 3.5). Lambda expressions almost entirely replace anonymous methods (from C# 2.0) although they don't support the notion of "I don't care about the parameters" in the way that anonymous methods do.

编辑:更多信息:lambda表达式是一段语法糖,可以创建委托实例或表达式树(后者是.NET 3.5的新手)。 Lambda表达式几乎完全取代了匿名方法(来自C#2.0),尽管它们不像匿名方法那样支持“我不关心参数”的概念。

#2


1  

That will be for a lambda expression:

这将是一个lambda表达式:

http://msdn.microsoft.com/en-us/library/bb397687.aspx

An example is here:

这里有一个例子:

MyControl.OnMouseDown += (sender, e) =>
{
  // Do something in the mouse down event
};

Here I have created a lambda expression event delegate. It basically saves me from having to create a separate function for it in the class.

在这里,我创建了一个lambda表达式事件委托。它基本上使我不必在类中为它创建一个单独的函数。

#3


1  

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

lambda表达式是一个匿名函数,可以包含表达式和语句,可用于创建委托或表达式树类型。

All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block

所有lambda表达式都使用lambda运算符=>,它被读作“转到”。 lambda运算符的左侧指定输入参数(如果有),右侧包含表达式或语句块

http://msdn.microsoft.com/en-us/library/bb397687.aspx

#4


1  

The => token is called the lambda operator.

=>标记称为lambda运算符。

It is used in lambda expressions to separate the input variables on the left side from the lambda body on the right side.

它在lambda表达式中用于将左侧的输入变量与右侧的lambda体分开。

MSDN