Edit

Lambda expression (=>) operator defines a lambda expression

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.

The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.

The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.

Tip

To find when a feature was first introduced in C#, consult the article on the C# language version history.

Lambda operator

In lambda expressions, the lambda operator => separates the input parameters on the left side from the lambda body on the right side.

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

Input parameters of a lambda expression are strongly typed at compile time. When the compiler infers the types of input parameters, like in the preceding example, you can omit type declarations. If you need to specify the type of input parameters, you must specify the type 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

The following example shows how to define a lambda expression without input parameters:

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

For more information, see Lambda expressions.

Expression body definition

An expression body definition uses the following general syntax:

member => expression;

For a member that returns a value, the expression's result must be implicitly convertible to the member's return type. For a void member, constructor, finalizer, or set, init, add, or remove accessor, the body must be a statement expression. A statement expression can be an assignment, method invocation, object creation, increment or decrement operation, or await expression. Its result, if any, is discarded.

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();
}

You can use expression body definitions for the following members:

  • Methods and local functions: A member that returns a value has the form T M() => expression;. A void member has the form void M() => statementExpression;. For more information, see Methods and Local functions.
  • Operators: An operator has the form public static T operator +(T left, T right) => expression;. For more information, see Operator overloading.
  • Properties and indexers: A read-only property or indexer has the form T P => expression; or T this[int i] => expression;. You can also use expression bodies for individual accessors. A get accessor has the form get => expression;. A set or init accessor has the form set => statementExpression; or init => statementExpression;. For more information, see Properties and Indexers.
  • Constructors and finalizers: These members have the form C() => statementExpression; or ~C() => statementExpression;. For more information, see Constructors and Finalizers.
  • Event accessors: An add or remove accessor has the form add => statementExpression; or remove => statementExpression;. For more information, see Events.

Operator overloadability

You can't overload the => operator.

C# language specification

For more information about the lambda operator, see the Anonymous function expressions section of the C# language specification.

See also