1Anonymous Methods and Lambdas
Hi friends, in this article we will learn about anonymous methods and lambda expressions in C#. Both let us write a function without giving it a separate method name. They are useful when passing small pieces of logic to another method, especially with events, LINQ and collections. An anonymous method uses the delegate keyword. A lambda expression uses the => operator and usually gives us shorter, cleaner syntax.
2Think About Sorting Products
Imagine an online shopping application. You have a list of products and want to display only products costing more than ₹1,000.
You could create a separate method just for this condition. But if the logic is small and used only once, you can pass it directly as a function. An anonymous method or lambda expression makes this possible.
3How the Two Approaches Work
1. Anonymous method
It uses the delegate keyword followed by a parameter list and a code block. It does not need a separate method name.
2. Lambda expression
It uses the => operator. The left side contains parameters and the right side contains an expression or code block.
3. Lambda shorthand
If the body contains one expression, you can write it without braces and return. The compiler understands the result.
4. Where are they used?
They are common in LINQ queries, event handlers, sorting and filtering collections.
| Anonymous method | Lambda |
|---|---|
| delegate(p) { return p.Price > 1000; } | p => p.Price > 1000 |
| Uses delegate keyword | Uses => operator |
| More syntax | Usually shorter |
4Compare Both in One Program
This example filters products using an anonymous method first and a lambda expression next. Both return the same matching products.
using System;
using System.Collections.Generic;
using System.Linq;
class Product
{
public string Name { get; set; }
public int Price { get; set; }
}
class Program
{
static void Main()
{
var products = new List<Product>
{
new() { Name = "Mouse", Price = 500 },
new() { Name = "Keyboard", Price = 1500 },
new() { Name = "Monitor", Price = 8000 }
};
var anonymous = products.Where(
delegate(Product p)
{ return p.Price > 1000; });
var lambda = products.Where(
p => p.Price > 1000);
Console.WriteLine("Anonymous method:");
foreach (var p in anonymous)
Console.WriteLine(p.Name);
Console.WriteLine("Lambda expression:");
foreach (var p in lambda)
Console.WriteLine(p.Name);
}
}
5Check the Printed Result
Mouse costs ₹500, so it is excluded. Keyboard and Monitor match the condition in both approaches.
Anonymous method: Keyboard Monitor Lambda expression: Keyboard Monitor
6Follow Both Expressions in VS Code
Watch the anonymous method and lambda expression apply the same price condition and produce matching results.
7What You Should Remember
Anonymous methods and lambda expressions both let you pass logic without creating a separate named method. Lambdas provide a shorter syntax and are widely used in modern C#.
- Anonymous method: Uses delegate.
- Lambda: Uses the => operator.
- Common use: LINQ, events and collection operations.
8Interview Questions to Practise
What is an anonymous method?
Answer: It is a method without a declared name, written inline using the delegate keyword.
What is a lambda expression?
Answer: It is a concise way to write an inline function using the => operator.
What is the difference between anonymous methods and lambdas?
Answer: Anonymous methods use delegate syntax. Lambdas use => syntax and often require less code.
Where are lambda expressions commonly used?
Answer: They are widely used with LINQ, event handlers, sorting and filtering collections.
Can a lambda expression contain multiple statements?
Answer: Yes. Use braces for a statement body. If a value is required, use an explicit return statement.