Lambda Expressions vs. Delegates in C#: A Comparison of Function References

Compare and contrast lambda expressions and delegates in C#, two powerful mechanisms for working with methods as variables. This tutorial clarifies their similarities, differences, and respective use cases in callback functions, event handling, and asynchronous programming.



Lambda Expressions vs. Delegates in C#

Understanding Delegates in C#

In C#, a delegate is a type that represents a reference to a method. It's like a function pointer; it allows you to store a reference to a method and then call that method indirectly. Delegates are frequently used for callbacks, event handling, and asynchronous programming. They are declared with a return type, a name, and a parameter list, similar to a standard method declaration.

Delegate Syntax

The syntax for declaring a delegate is:

delegate DelegateName();

Where:

  • return_type: The data type returned by the method the delegate references (void if no return value).
  • DelegateName: The name of the delegate type.
  • parameter_list: The parameters that the method will accept.
Example C# Code

public delegate int MathOperation(int a, int b);

Example: Using Delegates

This example shows how to create and use a delegate.

C# Code

using System;

public delegate int MathOperation(int a, int b);

public class DelegateExample {
    public static int Add(int x, int y) { return x + y; }
    public static int Subtract(int x, int y) { return x - y; }

    public static void Main(string[] args) {
        MathOperation addDelegate = Add;
        // ... rest of the code ...
    }
}

Understanding Lambda Expressions

Lambda expressions provide a more concise way to create anonymous functions (functions without a name). They're often used with delegates, providing a shorter and more readable syntax. Lambda expressions are particularly useful for short, inline functions.

Lambda Expression Syntax

The basic syntax is:

(parameters) => expression or (parameters) => { statements; }

Where:

  • parameters: The input parameters (enclosed in parentheses).
  • =>: The lambda operator (separates parameters from the expression/statements).
  • expression: A single expression (the function's return value).
  • { statements; }: A block of statements (if multiple statements are needed).

Example: Lambda Expressions

C# Code (Lambda Expressions)

Func<int, int, int> add = (x, y) => x + y;
Func<int, int> square = x => x * x;

Delegates vs. Lambda Expressions: Key Differences

Feature Lambda Expression Delegate
Definition Anonymous inline function. Type representing a method reference.
Parameters Can capture outer variables (closures). Explicitly defined in the delegate declaration.
Type Inference Type is inferred by the compiler. Type must be explicitly specified.
Invocation Used to create delegate instances directly. Requires creating a delegate instance and passing a method reference.
Common Uses LINQ queries, event handlers. Callbacks, event handling.