=> 运算符(C# 参考)=> operator (C# reference)

=> 令牌支持两种形式:作为 lambda 运算符、作为成员名称的分隔符和表达式主体定义中的成员实现。The => token is supported in two forms: as the lambda operator and as a separator of a member name and the member implementation in an expression body definition.

lambda 运算符Lambda operator

lambda 表达式中,lambda 运算符 => 将左侧的输入参数与右侧的 lambda 主体分开。In lambda expressions, the lambda operator => separates the input parameters on the left side from the lambda body on the right side.

以下示例使用带有方法语法的 LINQ 功能来演示 lambda 表达式的用法:The following example uses the LINQ feature with method syntax to demonstrate the usage of lambda expressions:

string[] words = { "bot", "apple", "apricot" };
int minimalLength = words
  .Where(w => w.StartsWith("a"))
  .Min(w => w.Length);
Console.WriteLine(minimalLength);   // output: 5

int[] numbers = { 4, 7, 10 };
int product = numbers.Aggregate(1, (interim, next) => interim * next);
Console.WriteLine(product);   // output: 280

lambda 表达式的输入参数在编译时是强类型。Input parameters of a lambda expression are strongly typed at compile time. 当编译器可以推断输入参数的类型时,如前面的示例所示,可以省略类型声明。When the compiler can infer the types of input parameters, like in the preceding example, you may omit type declarations. 如果需要指定输入参数的类型,则必须对每个参数执行类型声明,如以下示例所示:If you need to specify the type of input parameters, you must do that for each parameter, as the following example shows:

int[] numbers = { 4, 7, 10 };
int product = numbers.Aggregate(1, (int interim, int next) => interim * next);
Console.WriteLine(product);   // output: 280

以下示例显示如何在没有输入参数的情况下定义 lambda 表达式:The following example shows how to define a lambda expression without input parameters:

Func<string> greet = () => "Hello, World!";
Console.WriteLine(greet());

有关详细信息,请参阅 Lambda 表达式For more information, see Lambda expressions.

表达式主体定义Expression body definition

表达式主体定义具有下列常规语法:An expression body definition has the following general syntax:

member => expression;

其中 expression 是有效的表达式。where expression is a valid expression. expression 的返回类型必须可隐式转换为成员的返回类型。The return type of expression must be implicitly convertible to the member's return type. 如果成员的返回类型是 void,或者如果成员是构造函数、终结器或属性 set 访问器,则 expression 必须是语句表达式,其可以是任意类型。If the member's return type is void or if the member is a constructor, a finalizer, or a property set accessor, expression must be a statement expression, which can be of any type.

以下示例演示了用于 Person.ToString 方法的表达式主体定义:The following example shows an expression body definition for a Person.ToString method:

public override string ToString() => $"{fname} {lname}".Trim();

它是以下方法定义的简写版:It's a shorthand version of the following method definition:

public override string ToString()
{
   return $"{fname} {lname}".Trim();
}

自 C#6 起,支持方法、运算符和只读属性的表达式主体定义。Expression body definitions for methods, operators, and read-only properties are supported beginning with C# 6. 自 C# 7.0 起,支持构造函数、终结器、属性和索引器访问器的表达式主体定义。Expression body definitions for constructors, finalizers, and property and indexer accessors are supported beginning with C# 7.0.

有关详细信息,请参阅 “Expression-bodied 成员”。For more information, see Expression-bodied members.

运算符可重载性Operator overloadability

不能重载 => 运算符。The => operator cannot be overloaded.

C# 语言规范C# language specification

有关 lambda 运算符的详细信息,请参阅 C# 语言规范中的匿名函数表达式部分。For more information about the lambda operator, see the Anonymous function expressions section of the C# language specification.

另请参阅See also