Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

C# Anonymous Function Handler

1. Anonymous Function Handler

Let us say, we need to define a handler for the event and do not want to define a handler function in the class definition. In that case, one can go for the Anonymous Function Handler. We can define Anonymous Function Handler either using a Lambda Expression or using the Anonymous Delegate. In this example, we will try both the technique.

2. The Example

Below screen shows the example:

Fig 1. Form to Test the Example
Fig 1. Form to Test the Example

In the above example, we have a simple C# Windows Form with a button in it. We will hook the click event of the button with 1)External Handler 2)Anonymous Delegate 3)Lambda Expression. The example will help in learning all three techniques.

3. External Handler Method

Providing a separate method defined in the class is the default technique. Below is the code example for it.

Fig 2. External Handler Function
Fig 2. External Handler Function


  1. The IDE-generated code links the event “Click” with the handler function “btnClickMe_Click“. It passes the function name to the “EventHandler” delegate.
  2. Visual Studio provides you with an empty function definition, which was given to the “EventHandler” delegate in the previous step. Inside the body of the method, we show a message box stating that the function is handled.

4. Anonymous Delegate Handler

An Anonymous Delegate Handler is a powerful programming construct which allows you to define and use delegate functions without explicitly naming them. They are commonly used in event-driven programming, where you want to handle an event with a short, inline piece of code. Note, instead of defining a separate method, you can directly define the delegate handler right where it’s needed.

Anonymous delegate handlers provide a concise way to handle events and perform actions in response to certain events. They are especially useful when the event behavior is simple and doesn’t require a separate named method.

In the above example, we have a function btnClickMe_Click defined in the class definition. With Anonymous Delegate technique, we no need to define the function definition inside the class definition. To use Anonymous Delegate, we pass the function arguments to the delegate keyword and define the body in the same place. Now have a look at the example below:

Fig 3. Anonymous Delegate Handler Function
Fig 3. Anonymous Delegate Handler Function


  1. Just like our previous example, here we create the new instance of EventHandler. The Click event of the button stores this EventHandler instance.
  2. Instead of giving the function name, here we place the keyword delegate and pass the arguments object and EventArgs.
  3. The handler function body is defined to show a message stating event is handled. Here, the handler function is called Anonymous Delegate Handler Function. Since, it is anonymous the handler function cannot be removed once tied to the Click event.

5. Anonymous Handler via Lambda

A Lambda Expression is a brief way to define Anonymous Functions in C#. It allows you to create delegates or expression trees without explicitly defining a separate method. Lambda expressions are commonly used in LINQ queries and as event handlers. The syntax for a Lambda Expression in C# is as follows:

(input parameters) => expression or statement block

In the Lambda expression, we define the parameter list in the left side of the => operator and define the function body in the right-hand side. This is another technique to provide Anonymous Handler to the event. Now have look at the example below:

Fig 4. Lambda Expression Handler Function
Fig 4. Lambda Expression Handler Function


  1. Like in our past examples, we create a new instance of the EventHandler and hand over that to the Click event of the Button object placed in the windows form.
  2. The parameter list is packed in the right side of the => operator.
  3. The function body defined here shows the message box to the user stating the event is handled.

6. Running the Example

In a Windows form, you can register multiple event handler functions to the Click event of a button. When you click the button, C# will call the handler functions one by one and shows message boxes on the screen.

7. Code Reference

Categories: C#

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.