Lambda vs Delegate in C#: Key Differences and When to Use Each
delegate is a type that represents references to methods with a specific signature, while a lambda expression is a concise way to write anonymous functions that can be assigned to delegates or expression trees. Lambdas provide simpler syntax and inline function definitions, but delegates are the underlying type that stores method references.Quick Comparison
This table summarizes the main differences between lambda expressions and delegates in C#.
| Aspect | Delegate | Lambda Expression |
|---|---|---|
| Definition | A type that holds references to methods with a specific signature | An anonymous function syntax that can be assigned to delegates or expression trees |
| Syntax | Explicit method or anonymous delegate syntax | Concise inline syntax using => operator |
| Usage | Can reference named or anonymous methods | Defines anonymous methods inline, often used with LINQ |
| Flexibility | Supports multicast (multiple methods) | Represents a single method, but can be assigned to multicast delegates |
| Performance | Slightly faster in some cases due to direct method references | May generate additional compiler-generated classes |
| Introduced in C# | From the beginning (C# 1.0) | Introduced in C# 3.0 |
Key Differences
Delegates are types that define a method signature and can hold references to one or more methods matching that signature. They can point to named methods or anonymous methods defined with the delegate keyword. Delegates support multicast, meaning they can invoke multiple methods in order.
Lambda expressions are a newer, more concise syntax introduced in C# 3.0 to write anonymous functions inline. They use the => operator and can capture variables from the surrounding scope, making them very useful in LINQ queries and event handling. Lambdas are compiled into delegates or expression trees depending on context.
While delegates are the underlying type, lambdas provide a cleaner and more readable way to create anonymous methods. Delegates can be instantiated explicitly or implicitly, but lambdas always create anonymous functions. Also, lambdas can capture local variables (closures), which is harder to do with traditional anonymous delegates.
Delegate Code Example
using System; class Program { delegate int Operation(int x, int y); static int Add(int a, int b) { return a + b; } static void Main() { // Using named method Operation op1 = Add; Console.WriteLine(op1(3, 4)); // Using anonymous delegate Operation op2 = delegate(int x, int y) { return x * y; }; Console.WriteLine(op2(3, 4)); } }
Lambda Equivalent
using System; class Program { delegate int Operation(int x, int y); static void Main() { // Using lambda expression Operation op1 = (x, y) => x + y; Console.WriteLine(op1(3, 4)); Operation op2 = (x, y) => x * y; Console.WriteLine(op2(3, 4)); } }
When to Use Which
Choose lambda expressions when you want concise, inline anonymous functions, especially for short operations or when using LINQ queries. Lambdas improve readability and allow capturing variables from the surrounding scope easily.
Choose delegates when you need to define a reusable method signature type, support multicast invocation, or when working with legacy code that uses explicit delegate instances. Delegates provide more control over method references and invocation lists.