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:

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.
Explanation
- The IDE-generated code links the event “Click” with the handler function “btnClickMe_Click“. It passes the function name to the “EventHandler” delegate.
- 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:

Explanation
- Just like our previous example, here we create the new instance of EventHandler. The Click event of the button stores this EventHandler instance.
- Instead of giving the function name, here we place the keyword
delegate
and pass the argumentsobject
andEventArgs
. - 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:

Explanation
- 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.
- The parameter list is packed in the right side of the => operator.
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WinformTester { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnClickMe_Click(object sender, EventArgs e) { //Sample 01: Handled by IDE provided Handler Function MessageBox.Show("IDE Handler Function"); } private void Form1_Load(object sender, EventArgs e) { //Sample 02: Anonymous Delegate Handler btnClickMe.Click += new System.EventHandler( delegate (object sender1, EventArgs e1) { MessageBox.Show("Handled by Anonymous Delegate Function"); } ); //Sample 03: Lambda Expression Handler btnClickMe.Click += new System.EventHandler( (object sender2, EventArgs e2) => { MessageBox.Show("Lambda Expression Handler"); } ); } } } |
Categories: C#
Tags: Anonymous Delegate, C#, Event Handler, Lambda Expression