.NET Framework
lambdas
delegates
C# programming
software development

What is the difference between lambdas and delegates in the .NET Framework?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Delegates and lambdas are related in C#, but they are not the same thing. A delegate is a type that represents a callable method signature, while a lambda is a compact syntax for writing an anonymous function that can often be assigned to a delegate.

Delegates Are Types

A delegate defines the shape of a callable target.

csharp
1using System;
2
3public delegate int MathOperation(int left, int right);
4
5class Program
6{
7    static int Add(int a, int b) => a + b;
8
9    static void Main()
10    {
11        MathOperation op = Add;
12        Console.WriteLine(op(2, 3));
13    }
14}

MathOperation is a real .NET type. It says:

  • two int inputs
  • one int output

Any method matching that signature can be referenced by that delegate type.

Built-in delegate types such as Action and Func are just common reusable versions of the same idea.

Lambdas Are Syntax For Anonymous Functions

A lambda is a concise way to write a function inline:

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        Func<int, int, int> add = (a, b) => a + b;
8        Console.WriteLine(add(2, 3));
9    }
10}

Here the lambda (a, b) => a + b is not a delegate type. It is an anonymous function expression. The compiler can convert it to a compatible delegate type such as Func<int, int, int>.

That is the key distinction:

  • delegate is the target type
  • lambda is the code expression

Lambdas Usually End Up As Delegates

In everyday C# code, lambdas are often assigned to delegates:

csharp
MathOperation multiply = (x, y) => x * y;
Console.WriteLine(multiply(4, 5));

This makes it easy to blur the concepts, because the lambda is immediately converted into a delegate instance.

But the conversion is still a conversion. The lambda and the delegate are not interchangeable ideas.

Lambdas Can Also Become Expression Trees

One reason the distinction matters is that lambdas do not always compile to delegates. They can also compile to expression trees.

csharp
1using System;
2using System.Linq.Expressions;
3
4Expression<Func<int, int>> square = x => x * x;
5Console.WriteLine(square);

This does not store an executable delegate directly. It stores a data structure describing the code. Frameworks such as LINQ providers use this to inspect query logic.

A delegate cannot do that by itself. It represents executable method references, not a parseable code tree.

So lambdas are more flexible at the syntax level than delegates are at the type level.

Why Delegates Still Matter

Delegates are the foundation behind:

  • event handlers
  • callbacks
  • LINQ predicates
  • 'Func and Action'

For example:

csharp
1using System;
2
3class Button
4{
5    public event Action? Clicked;
6
7    public void Click() => Clicked?.Invoke();
8}
9
10class Program
11{
12    static void Main()
13    {
14        var button = new Button();
15        button.Clicked += () => Console.WriteLine("Clicked");
16        button.Click();
17    }
18}

The event is delegate-based. The lambda is just a convenient way to supply the handler.

Before lambdas, C# had anonymous methods:

csharp
1Func<int, int, int> add = delegate(int a, int b)
2{
3    return a + b;
4};

This is another way to create a delegate-compatible anonymous function. Lambdas are the more modern and concise syntax, but they serve a similar role.

That is another hint that lambdas are about expression syntax, not about replacing the delegate concept entirely.

A Good Mental Model

Keep this separation in mind:

  • delegate answers "what callable shape is expected"
  • lambda answers "what inline behavior do I want to provide"

Once you see it that way, confusing examples become easier to reason about.

For example, if a method asks for Func<string, bool>, that is a delegate type requirement. Supplying s => s.Length > 3 is one way to satisfy it.

Common Pitfalls

The biggest mistake is saying lambdas and delegates are the same thing. They are connected, but one is a type concept and the other is a syntax concept.

Another mistake is forgetting that lambdas can also become expression trees. In LINQ-to-Objects code that distinction is easy to ignore, but ORMs and query providers depend on it heavily.

People also assume custom delegates are obsolete because Func and Action exist. They are less common now, but still useful when you want clearer names or special semantics such as ref parameters.

Finally, when reading older C# code, remember that anonymous methods and lambdas solve similar problems with different syntax.

Summary

  • A delegate is a .NET type representing a callable signature.
  • A lambda is shorthand syntax for an anonymous function.
  • Lambdas are often converted to delegates, but they can also become expression trees.
  • Delegates power callbacks, events, and many standard library APIs.
  • The clearest mental model is type versus syntax: delegate defines the shape, lambda provides the behavior.

Course illustration
Course illustration

All Rights Reserved.