Delegates are the function to pointer. A delegate keeps the reference of function. Like as pointer in C/C++, same in C# we have delegates. Delegates in C# are reference types. They are type safe, managed function pointers in C# that can be used to invoke a method that the delegate refers to. The signature of the delegate should be the same as the signature of the method to which it refers.
The signature of a delegate type comprises of the following.
Let us see the working of delegates with this simple example. Here in this example, I am using a button click event. A message appears when the button is clicked.
Creation a button control can be seen below.
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(93, 71);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//Here we see that on clicking on button a Event Handler is calling named as button1_click
this.button1.Click += new System.EventHandler(this.button1_Click);
The form1.cs code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AnApplicationOfDelagatesNEvent
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi this is Button click event");
}
public void MyMessage( object sender , System.EventArgs e)
{
MessageBox.Show("Hi this from My Method");
}
}
}
Here on the button click event a message appears. Here I also write a method.
Here if user run the application and click on button then:

Figure 1.
Now I am calling MyMessage on the click of Button. Here we see that now if user click on button then the defination of button1_click will not be called because here Event is calling from MyMessage.
this.button1.Click += new System.EventHandler(MyMessage);

Figure 2.