Delegates and events in C#

Hello, and welcome to my fourth blog post. In this entry I will be talking about delegates in C#, as well as event handling.


A delegate is a reference to a method, allowing a programmer to assign that method to a variable. This concept does not exist in Java, but it is similar to function pointers in C. Delegates have several types that can be used, depending on the number of parameters and whether or not they return a value. Some of the types are:


Used for methods that have one parameter, and no return value.


Checks if a condition is true in an object, and will always return a Boolean.


Accepts any number of parameters and always returns a result.

Delegates can also be evaluated, allowing you to combine multiple methods into one delegate, allowing them to be called at the same time.

Below is a sample delegate, showing some of the syntax and functionality behind them.


class DelegateDemo
//Declare the delegate type
delegate void customDel(string value);
   static void Method1(string value)
Console.WriteLine(“My name is {0} !\n”, value);
   static void Method2(string value)
Console.WriteLine(“Hello {0} !\n”, value);
   static void Main()
//Create 3 variables of the custom delegate type
customDel firstMethod, secondMethod, thirdDel;
      //Assign the method to the delegate
firstMethod = Method1;
      Console.WriteLine(“Enter a string”);
String value = Console.ReadLine();
      Console.WriteLine(“Call the first method”);
      Console.WriteLine(“Call the second method”);
secondMethod = Method2;
      Console.WriteLine(“Call both at the same time!”);
//Example of delegate evalutation, allowing multiple methods to be evaulated
thirdDel = firstMethod + secondMethod;
      Console.WriteLine(“The second method was removed!”);
//Unassign the first delegate
thirdDel -= firstMethod;


Enter a string
Call the first method
My name is bob !
Call the second method
Hello bob !
Call both at the same time!
My name is bob !
Hello bob !
The second method was removed!
Hello bob !

Event handlers

Event handlers work with delegates to provide functionality. Once an event handler has been set up, they wait for something to happen, such as a button being pressed. After that, they are ‘triggered’, and activate the delegate, or event handler, that was assigned to them. Events are used in GUI applications to handle most of the functionality behind buttons or anything else that has user interaction. I’ll show that off in the demo below.

public partial class DemoForm : Form
//Declare custom delegate
public delegate void buttonClickHandler
(object sender, EventArgs e);
   public DemoForm()

//Create the form
//Add a button to the form
      Button button = new Button();
button.Parent = this;
button.Text = “Click it!”;
      //Set the location of the button
Point point = new Point(
(ClientSize.Width — button.Width) / 2, (ClientSize.Height — button.Height) / 2);
      button.Location = point;
      //Assign the clickHandler method to the delegate
buttonClickHandler handler = clickHandler;
      //Add the delegate as a handler
button.Click += new EventHandler(handler);
    public void clickHandler(object sender, EventArgs ea)
//Create a message box
MessageBox.Show(“The button has been clicked!”);

static void Main()
Application.Run(new DemoForm());
After the button has been clicked

One blog post is not enough to show what can be done with delegates and events. If you are interested, I encourage you to look more into the subject. This will be my final blog post, so thank you to everyone who read all of my posts!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.