Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The => token is the lambda declaration operator in C#. It serves as a syntactic separator that divides an input or pattern on its left side from an executable body or resulting expression on its right side. Technically, the => operator is right-associative and shares the same low precedence level as assignment operators (such as =, +=, and *=). It is utilized in three primary structural contexts within the C# language: defining lambda expressions (anonymous functions), defining expression-bodied members, and separating patterns from expressions in switch expressions.

Lambda Expressions

In a lambda expression, the => operator binds parameters to a delegate or an expression tree. Expression Lambda Syntax: Evaluates a single expression. It implicitly returns the result of the expression, except when the target delegate type returns void (such as an Action delegate), in which case the expression is simply executed without returning a value.
(input_parameters) => expression
Statement Lambda Syntax: Executes a block of statements enclosed in braces. Requires an explicit return statement if the target delegate expects a non-void return type.
(input_parameters) => { sequence_of_statements; }
Parameter Variations:
// Single implicitly typed parameter (parentheses are optional)
x => x * 2

// Multiple implicitly typed parameters
(x, y) => x + y

// Explicitly typed parameters
(int x, string y) => x.ToString() == y

// Parameterless
() => Console.WriteLine()

// Discards (ignoring parameters)
(_, _) => true

Expression-Bodied Members

The => operator allows for a concise syntactic definition of class or struct members that consist of a single expression. In this context, it replaces the traditional curly-brace body and return keyword. Syntax:
member_declaration => expression;
Structural Implementations:
// Method
public int Square(int x) => x * x;

// Read-only Property
public string FullName => $"{FirstName} {LastName}";

// Property with explicit accessors
public string Name
{
    get => _name;
    set => _name = value;
}

// Constructor
public Point(int x, int y) => (X, Y) = (x, y);

// Finalizer
~ResourceHandler() => Dispose(false);

// Indexer
public string this[int index] => _internalArray[index];

Switch Expressions

In switch expressions, the => operator acts as a separator between a match pattern and the expression that is evaluated and returned if the input matches that pattern. Syntax:
input_value switch
{
    pattern => expression,
    _ => default_expression
};

Type Resolution and Evaluation

The => operator itself does not have an intrinsic type. Its type resolution depends entirely on the context in which it is evaluated and the typing of its parameters. For implicitly typed lambdas (e.g., x => x * 2), the expression cannot exist in isolation. It requires a target type—meaning it must be cast or assigned to a compatible delegate type (e.g., Func<int, int>) or an Expression<TDelegate> so the compiler can resolve the types of the left-hand parameters and validate the right-hand body. Starting with C# 10, lambdas can possess a natural type. If the compiler can fully infer the parameter types and the return type (such as when parameters are explicitly typed), the lambda is assigned a natural delegate type. This allows the lambda to be assigned directly to an implicitly typed local variable (var) without requiring explicit casting or a predefined target type. Resolution Examples:
// Requires target typing (implicitly typed parameter)
Func<int, int> square = x => x * x;

// Natural type inference (C# 10+)
var naturalSquare = (int x) => x * x;        // Inferred as Func<int, int>
var printAction = () => Console.WriteLine(); // Inferred as Action
Master C# with Deep Grasping Methodology!Learn More