Anonymous Methods vs Lambda Expressions in C
This article explores the concept of anonymous methods in C# and examines how they compare directly to modern lambda expressions. You will learn the definitions of both constructs, their syntactical differences, functional capabilities such as expression tree conversion, and practical guidelines for choosing between them in modern software development.
What Are Anonymous Methods?
Introduced in C# 2.0, anonymous methods provide a way to pass a code
block as a delegate parameter without defining a separate, named method.
They are declared inline using the delegate keyword.
An anonymous method allows you to write executable code directly where the delegate is instantiated:
// Anonymous method example
Func<int, int, int> add = delegate(int a, int b)
{
return a + b;
};
int result = add(3, 4); // Returns 7A unique characteristic of anonymous methods is the ability to omit the parameter list entirely if the parameters are not used inside the body:
button.Click += delegate
{
Console.WriteLine("Button clicked!");
};What Are Lambda Expressions?
Introduced in C# 3.0 alongside LINQ, lambda expressions provide a
more concise and expressive syntax for creating anonymous functions.
They use the lambda operator =>, commonly read as "goes
to."
Lambdas come in two forms:
Expression Lambdas: Contains an expression as its body.
Func<int, int> square = x => x * x;Statement Lambdas: Contains a block of statements enclosed in braces.
Func<int, int, int> add = (a, b) => { return a + b; };
Key Differences
1. Syntax and Conciseness
Lambda expressions significantly reduce boilerplate code. Type inference allows you to omit parameter types in most cases, and single-parameter expressions do not require parentheses.
- Anonymous Method:
delegate(int x) { return x * 2; } - Lambda Expression:
x => x * 2
2. Expression Trees Support
Lambda expressions can be compiled into either executable delegates
(Func<T>, Action<T>) or into
expression trees (Expression<Func<T>>).
Expression trees allow code to be inspected and translated at runtime,
which is foundational for providers like Entity Framework to translate
C# queries into SQL.
Anonymous methods cannot be converted into expression trees; they can only be compiled into delegate instances.
3. Parameter Handling
Anonymous methods have a unique feature where parameters can be omitted entirely if they are not referenced:
// Valid anonymous method omitting EventArgs parameters
EventHandler handler = delegate { Console.WriteLine("Fired"); };Lambda expressions require matching the parameter signature of the delegate. However, modern C# allows discards to achieve a similar outcome:
// Lambda using discards
EventHandler handler = (_, _) => Console.WriteLine("Fired");4. Inline Evaluation
Lambda expressions support direct expression bodies, returning values
implicitly without the return keyword or enclosing braces.
Anonymous methods always require the delegate keyword,
braces { }, and explicit return statements
when returning values.
Comparison Summary
| Feature | Anonymous Methods | Lambda Expressions |
|---|---|---|
| Introduced In | C# 2.0 | C# 3.0 |
| Keyword/Operator | delegate keyword |
=> operator |
| Syntax Style | Verbose, block-based | Concise, expression or block |
| Expression Trees | No | Yes |
| Type Inference | Limited | Advanced |
| Parameter Omission | Yes (can omit parameter list) | Requires discards (_) |
Conclusion
Lambda expressions are the standard, modern replacement for anonymous methods in C#. They offer superior readability, cleaner syntax, and critical capabilities like expression trees for LINQ. While anonymous methods remain supported for backward compatibility, lambda expressions are preferred for almost all contemporary C# development.