---
title: "C# Anonymous Methods – A Beginner's Guide"  
description: "Anonymous methods in C# are a powerful feature that allows you to define inline methods without giving them a name."  
author: "Anubhav Sharma"  
published: 2025-04-22  
updated: 2025-04-22  
canonical: https://www.mindstick.com/blog/305506/c-sharp-anonymous-methods-a-beginner-s-guide  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# C# Anonymous Methods – A Beginner's Guide

**Anonymous methods** in C# are a powerful feature that allows you to define **inline methods** without giving them a name. These methods are typically used when you need to **pass behavior** as a parameter to a method or delegate, but don’t want to define a full method separately.

Anonymous methods are primarily used with **delegates**, **events**, and **LINQ expressions**.

## What is an Anonymous Method?

An **anonymous method** is a method without a name. Instead of defining a method separately, you write the method directly where it’s needed. Anonymous methods are often used with **delegates** and **events**.

## Syntax of an Anonymous Method

You define an anonymous method with the `delegate` keyword. Here's the basic syntax:

```cs
delegate(parameterList)
{
    // Method body
};
```

## Example

```cs
Action<string> greet = delegate(string name)
{
    Console.WriteLine("Hello, " + name);
};

greet("Alice"); // Output: Hello, Alice
```

## Example: Using Anonymous Methods with Delegates

Let’s say you have a delegate that expects a method to perform some action on an integer. Instead of defining a separate method, you can directly define the action using an anonymous method.

```cs
using System;

class Program
{
    delegate void ProcessNumber(int number);

    static void Main()
    {
        ProcessNumber process = delegate(int number)
        {
            Console.WriteLine("Processing number: " + number);
        };

        process(5);  // Output: Processing number: 5
    }
}
```

In this example:

1. `ProcessNumber` is a delegate type.
2. The anonymous method receives an integer and processes it.

## Anonymous Methods with Multiple Parameters

Anonymous methods can have [multiple parameters](https://www.mindstick.com/forum/160547/how-to-pass-multiple-parameters-in-url-dot-net-core-api), just like [regular methods](https://www.mindstick.com/forum/158923/what-is-the-role-of-constructors-in-c-sharp-classes-and-how-are-they-different-from-regular-methods).

```cs
using System;

class Program
{
    delegate int AddNumber(int x, int y);

    static void Main()
    {
        AddNumber add = delegate(int x, int y)
        {
            return x + y;
        };

        int result = add(5, 3);
        Console.WriteLine(result);  // Output: 8
    }
}
```

## Anonymous Methods with Events

Anonymous methods are also frequently used to [handle events](https://www.mindstick.com/forum/157865/how-does-knockoutjs-handle-events-and-event-binding) without needing to define a separate [event handler](https://www.mindstick.com/forum/1371/c-sharp-dot-net-event-handler-delegate-question) method.

```cs
using System;

class Button
{
    public event Action OnClick;

    public void Click()
    {
        OnClick?.Invoke();  // Trigger the event
    }
}

class Program
{
    static void Main()
    {
        Button button = new Button();

        // Attach an anonymous method to the event
        button.OnClick += delegate
        {
            Console.WriteLine("Button clicked!");
        };

        button.Click();  // Output: Button clicked!
    }
}
```

## Advantages of Anonymous Methods

1. **Concise**: No need to create a separate method.
2. **[Local Scope](https://www.mindstick.com/forum/34625/global-and-local-scope)**: Anonymous methods can be written where they are needed, making them easy to understand and maintain.
3. **Flexibility**: Especially useful when you don’t need to reuse the method elsewhere.

## Anonymous Methods vs Lambda Expressions

[Lambda expressions](https://www.mindstick.com/forum/159557/c-plus-plus-lambda-expressions-how-does-the-compiler-interpret-them), introduced in C# 3.0, are a more concise and flexible way of writing anonymous methods.

## [Lambda Expression](https://www.mindstick.com/articles/897/lambda-expression-in-c-sharp) Example:

```cs
Action<string> greet = name => Console.WriteLine("Hello, " + name);
greet("Alice");  // Output: Hello, Alice
```

**Key Differences**:

1. **Lambda expressions** are more compact than anonymous methods.
2. **Anonymous methods** use the `delegate` keyword, while **lambda expressions** use `=>`.

## Summary

| Concept | Description |
| --- | --- |
| What is it? | A method without a name |
| Syntax | `delegate` keyword |
| Use Case | Inline methods for [delegates and events](https://www.mindstick.com/forum/160401/describe-the-role-of-delegates-and-events-in-working-with-generic-methods-and-classes-in-c-sharp) |
| Flexibility | Often used in **delegates**, **LINQ**, and **events** |
| Difference with Lambdas | Lambdas are more concise and commonly used |

Anonymous methods are a great way to write compact, inline code for simple tasks. They're especially useful in [event handling](https://www.mindstick.com/articles/12724/event-handling-in-android) and delegating tasks dynamically. However, with the introduction of **lambda expressions**, many [developers prefer](https://answers.mindstick.com/qa/113975/why-do-developers-prefer-certain-programming-languages-for-specific-types-of-projects) lambdas for their clarity and brevity.

---

Original Source: https://www.mindstick.com/blog/305506/c-sharp-anonymous-methods-a-beginner-s-guide

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
