0
0
CsharpComparisonBeginner · 4 min read

Lambda vs Delegate in C#: Key Differences and When to Use Each

In C#, a 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#.

AspectDelegateLambda Expression
DefinitionA type that holds references to methods with a specific signatureAn anonymous function syntax that can be assigned to delegates or expression trees
SyntaxExplicit method or anonymous delegate syntaxConcise inline syntax using => operator
UsageCan reference named or anonymous methodsDefines anonymous methods inline, often used with LINQ
FlexibilitySupports multicast (multiple methods)Represents a single method, but can be assigned to multicast delegates
PerformanceSlightly faster in some cases due to direct method referencesMay 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

csharp
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));
    }
}
Output
7 12
↔️

Lambda Equivalent

csharp
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));
    }
}
Output
7 12
🎯

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.

Key Takeaways

Delegates are types that hold references to methods; lambdas are concise anonymous functions assigned to delegates.
Lambdas use simpler syntax and support closures, making them ideal for inline code and LINQ.
Delegates support multicast invocation, allowing multiple methods to be called in sequence.
Use lambdas for readability and quick anonymous methods; use delegates for explicit method references and multicast needs.