Introduction
In this tutorial, learn what a delegate is in C# and how delegates are implemented in C#. A delegate in C# is a type that is used to invoke a method. In this tutorial, you will learn the following.
- What is a delegate in C#?
- Why do we need delegates in C#?
- What are the benefits of delegates in C# and .NET?
- What are the different types of delegates in C#?
- How are delegates related to events in C#?
- What are single-cast and multicast delegates in C#?
- What is an anonymous delegate in C#?
- C# Delegate code examples
What is a Delegate in C#?
Delegate is one of the base types in .NET. Delegate is a class used to create and invoke delegates at runtime.

A delegate in C# is similar to a function pointer in C or C++. It's a new type of object in C#. A delegate is a very special type of object, as mentioned earlier. The entire object we used to define contained data, but a delegate contains the details of a method.
Why do we need delegates in C#?
C# programmers often need to pass a method as a parameter of other methods when dealing with events. For this purpose, we create and use delegates in C#. A delegate is a class that encapsulates a method signature. Although it can be used in any context, it often serves as the basis for the event-handling model in C# and .NET. One good way of understanding delegates is by thinking of a delegate as something that gives a name to a method signature.
Example
public delegate int DelegateMethod(int x, int y);
Any method that matches the delegate's signature, which consists of the return type and parameters, can be assigned to the delegate.
This makes it possible to programmatically change method calls and plug new code into existing classes. You can assign your own delegated method if you know the delegate's signature.
This ability to refer to a method as a parameter makes delegates ideal for defining callback methods.
Why do we need a Delegate?
In class, we create its object, which is an instance, but in delegate, when we create an instance, that is also referred to as a delegate (which means whatever you do, you will get a delegate).
The delegate does not know or care about the class of the object it references. Any object will do; all that matters is that the method's argument types and return types match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.
What are the benefits of delegates?
In simple words, delegates are object-oriented, type-safe, and very secure, as they ensure that the signature of the method being called is correct. Delegates make event handling simple and easy.
What are the types of delegates in C#?
There are two types of delegates in C#, singlecast delegates and multiplecast delegates.
- Singlecast delegate: Singlecast delegate points to a single method at a time. The delegate is assigned to a single method at a time. They are derived from System.Delegate class.
- Multicast Delegate: When a delegate is wrapped with more than one method, that is known as a multicast delegate.
In C#, delegates are multicast, meaning they can point to more than one function at a time. They are derived from System.MulticastDelegate class.
How to define a delegate in C#?
There are three steps in defining and using delegates.
Step 1. Declaration of a Delegate.
To create a delegate, you use the delegate keyword.
[attributes] [modifiers] delegate ReturnType Name ([formal-parameters]);
- The attributes factor can be a normal C# attribute.
- The modifier can be one or an appropriate combination of the following keywords: new, public, private, protected, or internal.
- The ReturnType can be any of the data types we have used. It can also be a type void or the name of a class.
- The Name must be a valid C# name.
Because a delegate is a definition for a method, you must use parentheses, which is required for every method. If this method does not take any argument, leave the parentheses empty.
Example
public delegate void DelegateExample();
The above code is how a delegate with no parameters is defined.
Step 2. Instantiation of Delegate.
DelegateExample d1 = new DelegateExample(Display);
The above code shows how a delegate is initiated.
Step 3. Invocation of Delegate.
d1();
The above code piece invokes a delegate d1().
What is a Singlecast delegate in C#?
A sample code demonstrates how to create and use a singlecast delegate.
using System;
namespace ConsoleApplication5
{
class Program
{
public delegate void delmethod();
public class P
{
public static void display()
{
Console.WriteLine("Hello!");
}
public static void show()
{
Console.WriteLine("Hi!");
}
public void print()
{
Console.WriteLine("Print");
}
}
static void Main(string[] args)
{
// here we have assigned static method show() of class P to delegate delmethod()
delmethod del1 = P.show;
// here we have assigned static method display() of class P to delegate delmethod() using new operator
// you can use both ways to assign the delegate
delmethod del2 = new delmethod(P.display);
P obj = new P();
// here first we have create instance of class P and assigned the method print() to the delegate i.e. delegate with class
delmethod del3 = obj.print;
del1();
del2();
del3();
Console.ReadLine();
}
}
}

Bounty PWPosted Feb 17, 2021, 8:54 PM
I search my question on google, search for c-sharpcorner, then i enter to the best tutorial!!
Jin NecesarioPosted Oct 3, 2020, 12:57 AM
Easy to read and direct to the point. Thanks and keep it up!
shashank pandeyPosted Apr 29, 2020, 2:39 AM
Obj.RaiseEvent(20, 10); here you called RaisedEvent method of XX class then why other method called by event delegates . you can directly call all method by object name .
prabhat kumarPosted Mar 13, 2020, 4:00 AM
Nice article
Rajanikant HawaldarPosted Mar 5, 2020, 8:24 PM
Spelling mistake 'delefates', Helpful article.
r vijayPosted Aug 26, 2019, 7:25 AM
Nice article. Please correct the spelling mistake "definituon"
monil thakorPosted Mar 11, 2016, 8:01 AM
Good