Loading...

Lambda Expressions in C#

Learn lambda expressions in C#, including concise syntax, Func and Action delegates, and practical LINQ usage examples.

In C#, lambda expressions allow writing anonymous (unnamed) methods in a short and readable way. They are defined using the => (lambda operator). Lambda expressions are especially common in LINQ queries, event subscriptions, and functional programming styles.


Basic Lambda Syntax

A lambda expression consists of a parameter list and a body. For simple expressions, curly braces are not required; when using a multi-line body, braces and return can be written.


// Single parameter, single-line expression
Func<int, int> square = x => x * x;
Console.WriteLine(square(5)); // 25

// Lambda with multiple parameters
Func<int, int, int> add = (a, b) => a + b;
Console.WriteLine(add(3, 4)); // 7

// Multi-line lambda
Func<int, int, int> multiply = (a, b) =>
{
    Console.WriteLine($"Multiplying: {a} * {b}");
    return a * b;
};
Console.WriteLine(multiply(2, 6)); // 12

Lambda with Action and Func

Lambda expressions are commonly used with Action and Func types:


Action greet = () => Console.WriteLine("Hello!");
greet(); // Hello!

Func<string, int> length = s => s.Length;
Console.WriteLine(length("Lambda")); // 6

Lambda and LINQ

Lambda expressions are most widely used in LINQ queries. They make it easy to filter, sort, and project collections.


var numbers = new List<int> { 1, 2, 3, 4, 5, 6 };

// Filter even numbers
var evens = numbers.Where(x => x % 2 == 0);

Console.WriteLine(string.Join(", ", evens)); // 2, 4, 6

// Calculate squares
var squares = numbers.Select(x => x * x);
Console.WriteLine(string.Join(", ", squares)); // 1, 4, 9, 16, 25, 36

Using Lambda with Events

Lambda expressions provide a practical way to handle events. This allows capturing an event concisely without defining a separate method.


public class Button
{
    public event EventHandler? Click;

    public void SimulateClick()
    {
        Console.WriteLine("Button clicked!");
        Click?.Invoke(this, EventArgs.Empty);
    }
}

class Program
{
    static void Main()
    {
        var btn = new Button();

        // Capture event with a lambda
        btn.Click += (s, e) => Console.WriteLine("Event: Button was clicked.");

        btn.SimulateClick();
    }
}

Closure and Variable Capture

Lambda expressions can access variables in their defining scope. This is called a closure.


int counter = 0;

Action increment = () =>
{
    counter++;
    Console.WriteLine($"Counter: {counter}");
};

increment(); // Counter: 1
increment(); // Counter: 2

Here, the lambda expression “captures” the outer counter variable and continues operating on it.


Example: Filtering and Reporting

In a store application, lambda expressions can be used to filter products by price and generate reports.


public class Product
{
    public string Name { get; set; } = string.Empty;
    public decimal Price { get; set; }
}

class Program
{
    static void Main()
    {
        var products = new List<Product>
        {
            new Product { Name = "Laptop", Price = 25000m },
            new Product { Name = "Mouse", Price = 300m },
            new Product { Name = "Keyboard", Price = 600m },
            new Product { Name = "Monitor", Price = 4500m }
        };

        // Find products above 1000
        var expensiveProducts = products.Where(p => p.Price > 1000);

        foreach (var p in expensiveProducts)
        {
            Console.WriteLine($"{p.Name} - {p.Price} USD");
        }

        // Calculate average price
        var avg = products.Average(p => p.Price);
        Console.WriteLine($"Average price: {avg:0.00} USD");
    }
}

TL;DR

  • Short, anonymous methods are defined with the => operator.
  • Commonly used with Action and Func.
  • Indispensable in LINQ for filtering, selecting, and ordering.
  • Can be used in event subscriptions without extra boilerplate.
  • Supports closures, allowing access to external variables.

Related Articles