Skip to content

Comparison with lambdas

Context: Lambdas are more concise and expressive than anonymous methods. They are the preferred way to write inline functions in modern C#.

// Anonymous method
Func<int, int> anon = delegate(int x) { return x * x; };
// Lambda expression
Func<int, int> lambda = x => x * x;
  • Expression tree conversion (Expression<Func<T>>)
  • Natural type inference
  • Shorter syntax for single‑expression bodies
Expression<Func<int, int>> expr = x => x * x; // works with lambda only
  • You need to omit parameters (delegate with no parameters but still accept any)
  • You are targeting C# 2.0
Action act = delegate { Console.WriteLine("No params"); };
// Lambda would require () => ...

Modern .NET code: Always prefer lambdas. Anonymous methods are only used for backward compatibility.

Example: Microsoft documentation recommends lambdas for new development.