> ## 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.

# C# Lambda

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.

```csharp theme={"dark"}
(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.

```csharp theme={"dark"}
(input_parameters) => { sequence_of_statements; }
```

**Parameter Variations:**

```csharp theme={"dark"}
// 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:**

```csharp theme={"dark"}
member_declaration => expression;
```

**Structural Implementations:**

```csharp theme={"dark"}
// 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:**

```csharp theme={"dark"}
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:**

```csharp theme={"dark"}
// 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
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
