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
- using System;
- namespace DelegateDemo {
- // Step-1: Define a delegate
- public delegate double AreaDelegate(double width, double height);
- public class Rectangle {
- public static double CalculateArea(double width, double height) {
- return (width * height);
- }
- }
- class DelegateExampleTwo {
- static void Main(string[] args) {
- //Step-2: Instantiating the delegate
- AreaDelegate area = new AreaDelegate(Rectangle.CalculateArea);
- // OR
- //Step-3: Binding method to delegate instance
- AreaDelegate area1 = Rectangle.CalculateArea;
- // Step-3: Invoking the Delegate by using invoke method
- Console.WriteLine("Using invoke method");
- Console.WriteLine("Area of Rectangle is {0}", area.Invoke(10.10, 20.10));
- // OR
- Console.WriteLine("-------------------------------------------------");
- // Step-3: Invoking the Delegate without using the invoke method
- Console.WriteLine("Without using invoke method");
- Console.WriteLine("Area of Rectangle is {0} ", area1(10.10, 20.10));
- Console.ReadLine();
- }
- }
- }
In the above example the delegate instance points to only one method like 'CalculateArea', so that type of delegate is said to be singlecast delegate.
Output

There are two ways of calling the multiple methods
- "+="
- "+"
Define a delegate.
Note
We can define the delegate inside the namespace or class.
- // Step-1: Define a delegate
- public delegate double RectangleDelegate(double width,double height);
- public class Rectangle {
- public static double CalculateArea(double width, double height) {
- return (width * height);
- }
- public static double CalculatePerimeter(double width, double height) {
- return 2 * (width + height);
- }
- }
Instantiating the delegate.
- //Step-2: Instantiating the delegate
- RectangleDelegate rectangle = Rectangle.CalculateArea;
- rectangle += Rectangle.CalculatePerimeter;
You can see in the above example, there is one delegate instance which points to more than one method.
Step 3
Invoking the delegate.
- // Step-3: Invoking the Delegate by using invoke method
- Console.WriteLine("Area of Rectangle is {0}", rectangle.Invoke(10.10, 20.10));
- Console.WriteLine("Perimeter of Rectangle is {0}", rectangle.Invoke(11.10, 21.22));
- using System;
- namespace DelegateDemo {
- // Step-1: Define a delegate
- public delegate double RectangleDelegate(double width, double height);
- public class Rectangle {
- public static double CalculateArea(double width, double height) {
- return (width * height);
- }
- public static double CalculatePerimeter(double width, double height) {
- return 2 * (width + height);
- }
- }
- class DelegateExampleTwo {
- static void Main(string[] args) {
- //Step-2: Instantiating the delegate
- RectangleDelegate rectangle = Rectangle.CalculateArea;
- rectangle += Rectangle.CalculatePerimeter;
- //Step-3: Invoking the Delegate by using invoke method
- Console.WriteLine("Area of Rectangle is {0}", rectangle.Invoke(10.10, 20.10));
- Console.WriteLine("Perimeter of Rectangle is {0}", rectangle.Invoke(11.10, 21.22));
- Console.ReadLine();
- }
- }
- }

Anonymous Method
You can simply say, it is a method without a name. We can use anonymous method in delegates. It is used to write less code. It has high performance as compared to normal delegate.
Advantages
- Write less code
- High performance
This is a simple example to understand the concepts of the anonymous method. We can take the above same example with only one method, 'CalculateArea'
Step 1
Define a delegate
- // Step-1: Define a delegate
- public delegate double RectangleDelegate(double width, double height);
Instantiating the delegate
Note
We can see in the below example there is not any method declaration. We have directly defined method body to the delegate.
- //Step-2: Instantiating the delegate by using anonymous method
- RectangleDelegate rectangle = delegate(double width, double height)
- {
- return (width * height);
- };
Invoking the delegate
- // Step-3: Invoking the Delegate
- onsole.WriteLine("Area of Rectangle is {0}", rectangle.Invoke(10.10, 20.10));
- using System;
- namespace DelegateDemo {
- // Step-1: Define a delegate
- public delegate double RectangleDelegate(double width, double height);
- class AnonymousMethod {
- static void Main(string[] args) {
- // Step-2: Instantiating the delegate by using anonymous method
- RectangleDelegate rectangle = delegate(double width, double height) {
- return (width * height);
- };
- // Step-3: Invoking the Delegate
- Console.WriteLine("Area of Rectangle is {0}", rectangle.Invoke(10.10, 20.10));
- Console.ReadLine();
- }
- }
- }
- It is used in Event
- It is used in Threading
- It is used to Callback Function
- It is used in Multicasting

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!