Introduction to C# Anonymous Methods

Written by Krunal Shah /

Anonymous Methods

Introduction

C# is a powerful programming language that is widely used for developing various types of applications. One of the features of C# that makes it so versatile is its support for anonymous methods. Anonymous methods are a way to create small, reusable code blocks that can be passed as arguments to other methods or used as event handlers. In this article, we will explore what anonymous methods are and how they can be used in C# programming.

What are Anonymous Methods?

An anonymous method is a method that does not have a name. Instead, it is defined inline, within the code where it is used. Anonymous methods are similar to lambda expressions, which were introduced in C# 3.0, but were available before that in C# 2.0 as anonymous methods. Anonymous methods allow developers to create small, reusable blocks of code that can be passed as arguments to other methods or used as event handlers without having to define a separate method.

How to Define Anonymous Methods

An anonymous method is defined using the delegate keyword followed by a set of parentheses that contain the method's parameters. The method's code is placed between curly braces. The basic syntax for defining an anonymous method is as follows:

CSHARP
delegate(parameters) { method body }

For example, the following code defines an anonymous method that takes a single parameter of type int and returns void:

CSHARP
delegate(int x) { Console.WriteLine(x); }

How to Use Anonymous Methods

There are several ways to use anonymous methods in C#. One of the most common ways is to pass them as arguments to other methods. For example, the following code shows how to use an anonymous method as an argument to the Array.ForEach method:

CSHARP
int[] numbers = { 1, 2, 3, 4, 5 };
Array.ForEach(numbers, delegate(int x) { Console.WriteLine(x); });

In this example, the Array.ForEach method is called with two arguments: the array of numbers and an anonymous method that takes a single parameter of type int and writes it to the console. The Array.ForEach method then calls the anonymous method for each element in the array, passing the element as the method's parameter.

Another way to use anonymous methods is as event handlers. For example, the following code shows how to use an anonymous method as the event handler for a button's Click event:

CSHARP
button.Click += delegate(object sender, EventArgs e) { MessageBox.Show("Button clicked"); };

In this example, the += operator is used to add the anonymous method to the button's Click event. The anonymous method takes two parameters: an object that represents the sender of the event and an EventArgs object that contains information about the event. The method then displays a message box with the text "Button clicked".

Anonymous Methods vs. Lambda Expressions

As mentioned earlier, anonymous methods are similar to lambda expressions. Both are used to create small, reusable blocks of code that can be passed as arguments to other methods or used as event handlers. The main difference between the two is the syntax used to define them. Anonymous methods use the delegate keyword, whereas lambda expressions use the => operator.

For example, the following code defines an anonymous method that takes two parameters and returns their sum:

CSHARP
delegate(int x, int y) { return x + y; }

Advantages of C# Anonymous Methods

There are several advantages to using anonymous methods in C#:

  • Simplicity: Anonymous methods are simple to use and understand. They don't require you to define a separate method, which can make your code more readable and easier to maintain.

  • Conciseness: Anonymous methods allow you to write code that is more concise and to the point. Because they don't have a name, they don't add unnecessary clutter to your code.

  • Reusability: Anonymous methods are reusable and can be passed as arguments to other methods or used as event handlers. This allows you to write code that is more modular and easier to maintain.

  • Flexibility: Anonymous methods can be used in a variety of situations, from simple loops to more complex event handling. This allows you to write code that is more flexible and adaptable to changing requirements.

  • Performance: Anonymous methods can improve performance by reducing the number of method calls required. Because they are defined inline, they don't add the overhead of a separate method call.

In conclusion, C# Anonymous Methods are a powerful feature of C# programming language that can help to make your code more readable, maintainable and adaptable. They allow for simple, concise, reusable, and flexible code. And can improve performance by reducing the number of method calls required.