LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

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.

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.

Remember Both approaches allow you to pass behaviour as a value instead of creating a separate named method.

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
Pro Tip Prefer lambda expressions for most modern C# collection and LINQ operations because they are concise and readable.

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.

Program.csC#
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);
    }
}
Common Mistake The lambda operator is =>, not = or ==. The left side represents parameters; the right side contains the logic.

5Check the Printed Result

Mouse costs ₹500, so it is excluded. Keyboard and Monitor match the condition in both approaches.

Output
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.

Program.cs - Visual Studio Code
▣⌕⑂▤
● Program.cs
 
PROBLEMSOUTPUTDEBUG CONSOLETERMINAL
 
Ln 1, Col 1UTF-8  C#
Preparing the two filtering approaches...

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

Q1Easy

What is an anonymous method?

Answer: It is a method without a declared name, written inline using the delegate keyword.

Q2Easy

What is a lambda expression?

Answer: It is a concise way to write an inline function using the => operator.

Q3Medium

What is the difference between anonymous methods and lambdas?

Answer: Anonymous methods use delegate syntax. Lambdas use => syntax and often require less code.

Q4Medium

Where are lambda expressions commonly used?

Answer: They are widely used with LINQ, event handlers, sorting and filtering collections.

Q5Hard

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.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.