What is the Delegate?
It is type safe function pointer which holds the reference/address of the method.
Type safe function means:
- The return type of delegate should be same as the return type of method.
- The parameter type of delegate should be the same as parameter type of method.
Step 1
Define a Delegate.
- [<modifier>] delegate void|type(return_type) <delegate_name>([<parameter list>]);
- public delegate void MessageDelegate();
Step 2
Instantiating the delegate or binding method to the delegate instance.
- <delegate_name> object_name = new <delegate_name>(<method_name>);
OR
- <delegate_name> object_name = <method_name>;
- // Step-2: Instantiating the Delegate.
- MessageDelegate message = new MessageDelegate(Email.DisplayMessage);
Note
<method_name> If the method is static then, we call it by using the class name. If the method is non-static then we can call that method by using object of the class.
There is two way to instantiate the delegate as mentioned above. I will go through one by one in the example so that you can understand better.
Step 3
Invoking the delegate.
- <delegate_object>.Invoke(<parameters>);
OR
- <delegate_object>(<parameters>);
- // Step-3: Invoking the delegate
- message.Invoke();
<parameters> We can pass parameters as we mentioned to the respective methods.
Example 1
This is a very simple example to understand the basic concepts of the delegate.
Note
We can define the delegate inside the namespace or class.
- // Step-1: Define a Delegate
- public delegate void MessageDelegate();
- public delegate string EmailDelegate(string strEmail);
Create class having names like "Email" and two methods as follow.
- public class Email {
- public static string GetEmail(string strEmail) {
- return "Your Email Id is " + strEmail;
- }
- public static void DisplayMessage() {
- Console.WriteLine("Welcome to CSharp Corner");
- }
- }
Step 2
Instantiating the delegate
- // Step-2: Instantiating the Delegate.
- EmailDelegate email = new EmailDelegate(Email.GetEmail);
- MessageDelegate message = new MessageDelegate(Email.DisplayMessage);
The second way to do the binding method to the delegate instance:
- //OR
- // Step-2: Binding method to the Delegate instance.
- EmailDelegate email = Email.GetEmail;
- MessageDelegate message = Email.DisplayMessage;
Step 3
Invoking the delegate or binding method to delegate instance.
- // Step-3: Invoking the delegate
- string strEmail= email.Invoke("[email protected]");
- Console.WriteLine(strEmail);
- message.Invoke();
We can invoke method by using "Invoke()" method or we can pass direct parameter to the delegate instance as following.
- // Step-3: Invoking the delegate second way
- email("[email protected]");
- message();
Program Code
- using System;
- namespace DelegateDemo {
- // Step-1: Define a Delegate
- public delegate void MessageDelegate();
- public delegate string EmailDelegate(string strEmail);
- public class Email {
- public static string GetEmail(string strEmail) {
- return "Your Email Id is " + strEmail;
- }
- public static void DisplayMessage() {
- Console.WriteLine("Welcome to CSharp Corner");
- }
- }
- class Program {
- static void Main(string[] args) {
- // Step-2: Instantiating the Delegate.
- EmailDelegate email = new EmailDelegate(Email.GetEmail);
- MessageDelegate message = new MessageDelegate(Email.DisplayMessage);
- // Step-3: Invoking the delegate
- string strEmail = email.Invoke("[email protected]");
- Console.WriteLine(strEmail);
- message.Invoke();
- Console.ReadLine();
- }
- }
- }

Type of delegate in c#
- Singlecast Delegate
- Multicast Delegate
Example 2
This is a simple example to understand the basic concept of 'single cast delegate'.
Program Code



Jitendra WaghalePosted Aug 3, 2018, 3:41 PM
Its very helpful, Thanks!!
Viknaraj ManogararajahPosted Jul 21, 2018, 11:03 PM
Nice Article, Thank you for sharing
Tukaram MarwalePosted May 9, 2018, 3:22 AM
@shrimant it helps me alot!