Lambda Expressions in C#
Source: Dev.to
Introduction
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.
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
Using Action and Func
Action– represents methods that do not return a value.Func– represents methods that return a result.
Action greet = () => Console.WriteLine("Hello!");
greet(); // Hello!
Func<string, int> length = s => s.Length;
Console.WriteLine(length("Lambda")); // 6
Lambda Expressions in LINQ
Lambdas 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
Event Handling with Lambdas
Lambdas provide a concise way to subscribe to events 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();
}
}
Closures
A lambda can capture variables from its defining scope, forming a closure.
int counter = 0;
Action increment = () =>
{
counter++;
Console.WriteLine($"Counter: {counter}");
};
increment(); // Counter: 1
increment(); // Counter: 2
Real‑World Example: Filtering Products
public class Product
{
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
}
class Program
{
static void Main()
{
var products = new List
{
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");
}
}
Summary
- Short, anonymous methods are defined with the
=>operator. - Commonly used with
ActionandFunc. - Indispensable in LINQ for filtering, selecting, and ordering.
- Useful for event subscriptions without extra boilerplate.
- Support closures, allowing access to external variables.