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

时间:2022-12-19 16:57:24

I just came over this syntax in some of the questions in this forum, but Google and any other searchengine tends to block out anything but letters and number in the search so it is impossible to search out "=>".

我刚刚在本论坛的一些问题中提到了这种语法,但Google和任何其他搜索引擎都会在搜索中阻止除字母和数字之外的任何内容,因此无法搜索“=>”。

So can anyone tell me what it is and how it is used?

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

7 个解决方案

#1


71  

It's the lambda operator.

这是lambda运算符。

From C# 3 to C# 5, this was only used for lambda expressions. These are basically a shorter form of the anonymous methods introduced in C# 2, but can also be converted into expression trees.

从C#3到C#5,这仅用于lambda表达式。这些基本上是C#2中引入的匿名方法的较短形式,但也可以转换为表达式树。

As an example:

举个例子:

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

is equivalent to:

相当于:

Func<Person, string> nameProjection = delegate (Person p) { return p.Name; };

In both cases you're creating a delegate with a Person parameter, returning that person's name (as a string).

在这两种情况下,您都要创建一个带有Person参数的委托,并返回该人的姓名(作为字符串)。

In C# 6 the same syntax is used for expression-bodied members, e.g.

在C#6中,相同的语法用于表达式身体成员,例如

// Expression-bodied property
public int IsValid => name != null && id != -1;

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

See also:

(And indeed many similar questions - try the lambda and lambda-expressions tags.)

(确实有很多类似的问题 - 试试lambda和lambda-expressions标签。)

#2


15  

It means awesomeness. For e.g.

这意味着很棒。对于例如

x => x + 1

represents a method which takes x as a parameter and returns the successor of it.

表示将x作为参数并返回其后继的方法。

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

assigns an event handler to a button by invoking a method that a MethodInfo holds.

通过调用MethodInfo包含的方法为按钮指定事件处理程序。

#3


11  

It is another form of function notation. The following are roughly equivalent:

它是函数符号的另一种形式。以下大致相同:

// explicit function
int MyFunc(int pParam)
{
   return pParam;
}

// delegate function
Func<int, int> MyFunc = delegate(int pParam) { return pParam; };

// lambda expression
Func<int, int> MyFunc = x => x;

Think of a lambda expression as saying, "given something, return something". In the example above, the function x => x says "given x, return x". (Although lambda expressions do not necessarily need to return something, in which case you might read them as "given x, do something with x".)

想想一个lambda表达式,“给出一些东西,返回一些东西”。在上面的例子中,函数x => x表示“给定x,返回x”。 (虽然lambda表达式不一定需要返回某些东西,在这种情况下,你可能会将它们读作“给定x,用x做一些事情”。)

#4


9  

here's a simple example from msdn

这是msdn的一个简单例子

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

Anything before the => are the input parameters, and anything after is the expression. You can have multiple input parameters. Lambdas are mainly used with Linq.

=>之前的任何内容都是输入参数,之后的任何内容都是表达式。您可以拥有多个输入参数。 Lambdas主要用于Linq。

#5


5  

Instead of using anonymous method like this:

而不是像这样使用匿名方法:

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

you simply write it like this:

你只需这样写:

somevar.Find(n => n < 10);

It will take the data type based on the return value.

它将根据返回值获取数据类型。

#6


0  

It basically means "goes into", like a parameter

它基本上意味着“进入”,就像一个参数

MyObjectReference => MyObjectReference.DoSomething()

Usually you use them to pass functions into methods as parameters, or in LINQ statements

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

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

For example.

#7


0  

It is part of the syntax of a lambda expression. A lambda expression is essentially a shortened form of a delegate or of an anonymous method. To illustrate, assume that I have an array of strings matching the letters of the alphabet. I could pick out the members of that array that contained values greater than "E" with the following LINQ expression:

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

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

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

The part of the lambda expression to the left of the "=>" identifies the variable name for the test (which is set to the individual members of alphabet) and the part of the lambda expression to the right of the "=>" identifies the processing. In this case the processing produces a boolean value that the Where logic uses to determine if each member of the alphabet is passed through to the someLetters array.

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

#1


71  

It's the lambda operator.

这是lambda运算符。

From C# 3 to C# 5, this was only used for lambda expressions. These are basically a shorter form of the anonymous methods introduced in C# 2, but can also be converted into expression trees.

从C#3到C#5,这仅用于lambda表达式。这些基本上是C#2中引入的匿名方法的较短形式,但也可以转换为表达式树。

As an example:

举个例子:

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

is equivalent to:

相当于:

Func<Person, string> nameProjection = delegate (Person p) { return p.Name; };

In both cases you're creating a delegate with a Person parameter, returning that person's name (as a string).

在这两种情况下,您都要创建一个带有Person参数的委托,并返回该人的姓名(作为字符串)。

In C# 6 the same syntax is used for expression-bodied members, e.g.

在C#6中,相同的语法用于表达式身体成员,例如

// Expression-bodied property
public int IsValid => name != null && id != -1;

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

See also:

(And indeed many similar questions - try the lambda and lambda-expressions tags.)

(确实有很多类似的问题 - 试试lambda和lambda-expressions标签。)

#2


15  

It means awesomeness. For e.g.

这意味着很棒。对于例如

x => x + 1

represents a method which takes x as a parameter and returns the successor of it.

表示将x作为参数并返回其后继的方法。

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

assigns an event handler to a button by invoking a method that a MethodInfo holds.

通过调用MethodInfo包含的方法为按钮指定事件处理程序。

#3


11  

It is another form of function notation. The following are roughly equivalent:

它是函数符号的另一种形式。以下大致相同:

// explicit function
int MyFunc(int pParam)
{
   return pParam;
}

// delegate function
Func<int, int> MyFunc = delegate(int pParam) { return pParam; };

// lambda expression
Func<int, int> MyFunc = x => x;

Think of a lambda expression as saying, "given something, return something". In the example above, the function x => x says "given x, return x". (Although lambda expressions do not necessarily need to return something, in which case you might read them as "given x, do something with x".)

想想一个lambda表达式,“给出一些东西,返回一些东西”。在上面的例子中,函数x => x表示“给定x,返回x”。 (虽然lambda表达式不一定需要返回某些东西,在这种情况下,你可能会将它们读作“给定x,用x做一些事情”。)

#4


9  

here's a simple example from msdn

这是msdn的一个简单例子

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

Anything before the => are the input parameters, and anything after is the expression. You can have multiple input parameters. Lambdas are mainly used with Linq.

=>之前的任何内容都是输入参数,之后的任何内容都是表达式。您可以拥有多个输入参数。 Lambdas主要用于Linq。

#5


5  

Instead of using anonymous method like this:

而不是像这样使用匿名方法:

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

you simply write it like this:

你只需这样写:

somevar.Find(n => n < 10);

It will take the data type based on the return value.

它将根据返回值获取数据类型。

#6


0  

It basically means "goes into", like a parameter

它基本上意味着“进入”,就像一个参数

MyObjectReference => MyObjectReference.DoSomething()

Usually you use them to pass functions into methods as parameters, or in LINQ statements

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

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

For example.

#7


0  

It is part of the syntax of a lambda expression. A lambda expression is essentially a shortened form of a delegate or of an anonymous method. To illustrate, assume that I have an array of strings matching the letters of the alphabet. I could pick out the members of that array that contained values greater than "E" with the following LINQ expression:

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

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

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

The part of the lambda expression to the left of the "=>" identifies the variable name for the test (which is set to the individual members of alphabet) and the part of the lambda expression to the right of the "=>" identifies the processing. In this case the processing produces a boolean value that the Where logic uses to determine if each member of the alphabet is passed through to the someLetters array.

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

相关文章