Dear reader, today we will discuss another important component of OOP, which is delegates. It's the modern approach to call methods. Using that approach you can call any method with the same signature as the delegate. So we can say that a delegate is the launching pad for methods; any method can be launched that matches the pattern. Or a Delegate is a class. When you create an instance of it, you pass in the function name (as a parameter for the delegate's constructor) to which this delegate will refer.

In simple words, a delegate is a type that safely encapsulates a method; a delegate is a pointer to a method. Just like you can pass a variable by reference, you can pass a reference to a method. Delegates are often used to implement callbacks and event listeners. A delegate does not need to know anything about the classes or methods it works with.

A Delegate consists of the following three parts:

Declare a Delegate

Instantiate the Delegate

Execute the Delegate

Simple Example 1:

Output:

Block A:

In this sessoion we have defined a delegate with the name Delegate_Pro with two parameters.

Block B:

In this sessoion we have defined the method totalcost with parameters.

In the implementation of that method we have the sum the two values and return that.

Block C:

First we have defined the object of the delegate passing method as the parameter.

Then we will input two parameters from the user and save them in two variables.

Now we will activate the delegate using the object we created and save the result in another variable.

In the end we will show the result in the console.

Events

A hook on an object where the code outside of the object can say "When that something happens, that fires this event, please call my code".

Simple Example 2:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace @event

{

publicpartialclassForm1 : Form

{

publicint a=1;

public Form1()

{

InitializeComponent();

this.button1.Click += new System.EventHandler(this.button1_Click);

this.button1.Click += new System.EventHandler(this.button1_Click);

}

privatevoid button1_Click(object sender, EventArgs e)

{

MessageBox.Show(a.ToString());

a++;

}

}

}

Output:

In the preceding example we have defined the simple event on button click. When we click the button once, it will display the message box three times or call the click event three times.

Delegate and Events

The best uses of delegates are in the events and we have daily use delegates with events without knowing them.

Simple Example 3:

Output:

Block A:

In this sessoion we have defined a delegate with the name OnEventHandler with two parameters.

Block B:

In this sessoion we have tagged the delegate with the event FiveEvent.

We have defined the method Gotfive. In that method we have activated the delegate using FiveEvent(this, EventArgs.Empty);

Another method callback having the same signature with delegates.

Block C:

In this sessoion we have defined an object of the class FEvent that is fevent.

Using that fevent we have instantiated the Delegate with the callback method.

Create the object of the Random class with the name of random.

We use a for loop and generate five numbers using the random object.

It will check the numbers; for each one that's five, it will activate the delegate.