Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Operators and Expressions

C# lambda operator =>

Reference for the C# => operator: lambda expressions, statement lambdas, expression-bodied members, discards, static lambdas, attributes and natural types.

The => operator has two jobs in C#. In a lambda expression it separates the parameter list from the body: x => x * 2 is an anonymous function that takes x and returns x * 2. In an expression-bodied member it replaces a block with a single expression: public int Area => Width * Height;.

Lambdas convert to delegate types (Func<>, Action<>, Predicate<>, your own delegates) and, when passed to certain APIs, to expression trees. Since C# 10 a lambda has a natural type when its parameters are declared, so var square = (int x) => x * x; compiles.

C#
Func<int, int> twice = x => x * 2;
Console.WriteLine(twice(21));   // 42

Try it

C# Lambda forms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Expression-bodied members and static lambdas

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
(parameters) => expression
(parameters) => { statements }
static (parameters) => ...
async (parameters) => ...
[Attribute] (Type name) => ...
ReturnType (Type name) => ...

public Type Member => expression;       // expression-bodied member
Part Description
Parameters Parenthesized list. Parentheses are optional for exactly one parameter without a type or modifier. Types are usually inferred from the target delegate.
=> Lambda arrow ("goes to").
Body A single expression (its value is the result) or a block with return.
static The lambda cannot capture locals or this (C# 9).
async The lambda can use await.

Returns: a delegate (or expression tree) instance. The delegate's own return type is the type of the expression or of the returned values.

Errors: the compiler reports an error if it cannot infer a delegate type (var f = x => x; fails because x has no type), if a statement lambda has no return on some path, or if an expression tree contains constructs it cannot represent (statement bodies, async, ?.).

Usage notes

  • Closures. A lambda captures variables, not copies of their values. In loops, for shares one variable across iterations while foreach creates a fresh one per iteration, which causes classic surprises.
  • Allocation. A lambda that captures nothing is cached by the compiler; one that captures state allocates a closure object each time. Use static to guarantee no capture.
  • Expression-bodied members work for methods, properties (getter-only), indexers, constructors, finalizers and operators. public int Count => _items.Count; is evaluated on every access; public int Count { get; } = 0; is a stored value. Confusing the two is a frequent mistake.
  • Natural type. A lambda with declared parameter types picks Func<>/Action<> automatically; otherwise it needs a target type. Method groups (list.ForEach(Console.WriteLine)) can replace trivial lambdas.
  • Discards. (_, _) => 0 ignores parameters; with more than one _ they are all discards.
  • this in lambdas refers to the enclosing instance, unlike JavaScript's function.
  • One token, several places. The same => token also separates the pattern from the result in switch expression arms.
  • Debugging. Lambda stack frames appear as compiler-generated names like <Main>b__0.