Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Understanding Delegates in C#

Understanding Delegates in C#

23 May 2024
Intermediate
150K Views
7 min read
Learn with an interactive course and practical hands-on labs

Best Free C# Course: Learn C# In 21 Days

Delegates in C#: An Overview

A delegate is a reference type that holds the reference of a class method. Any method that has the same signature as the delegate can be assigned to a delegate. It is very similar to the function pointer but with the difference that delegates are type-safe. We can say that it is the object-oriented implementation of function pointers. In this C# Tutorial, we will explore more about delegates which will include, types of delegates in c#, when to use delegates, and what is delegates in c#.

What are delegates?

There are three steps for defining and using delegates:

  1. Declaration

    A delegate is declared by using the keyword delegate, otherwise it resembles a method declaration.

  2. Instantiation

    To create a delegate instance, we need to assign a method (which has the same signature as a delegate) to delegate.

  3. Invocation

    Invoking a delegate is like invoking a regular method.

Example of delegates:

Let's elaborate delegates in C# Compiler with its example.

 //1. Declaration
public delegate int MyDelagate(int a, int b); //delegates having same signature as method 

public class Example
{
 // methods to be assigned and called by delegate
 public int Sum(int a, int b)
 {
 return a + b;
 }

 public int Difference(int a, int b)
 {
 return a - b;
 }
 }
 class Program
 {
 static void Main()
 {
 Example obj = new Example();
 
 // 2. Instantiation : As a single cast delegate
 MyDelagate sum = new MyDelagate(obj.Sum); 
 MyDelagate diff = new MyDelagate(obj.Difference);
 
 // 3.Invocation
 Console.WriteLine("Sum of two integer is = " + sum(10, 20)); 
 Console.WriteLine("Difference of two integer is = " + diff(20, 10));
 }
 }
 

Output

  Sum of two integer is = 30
 Difference of two integer is = 10  

Read More - C# Interview Questions And Answers

Key points about delegates

  1. Delegates are like C++ function pointers but are type-safe.

  2. Delegates allow methods to be passed as parameters.

  3. Delegates are used in event handling for defining callback methods.

  4. Delegates can be chained together i.e. these allow defining a set of methods that are executed as a single unit.

  5. Once a delegate is created, the method it is associated with will never change because delegates are immutable in nature.

  6. Delegates provide a way to execute methods at run-time.

  7. All delegates are implicitly derived from the System.MulticastDelegate, a class that is inherited from System.Delegate class.

  8. Delegate types are incompatible with each other, even if their signatures are the same. These are considered equal if they have the reference of the same method.

Types of delegates

  1. Single cast delegate

    A single cast delegate holds the reference of only a single method. In the previous example, the created delegate is a single-cast delegate.

  2. Multicast delegate

    A delegate who holds the reference of more than one method is called a multi-cast delegate. A multicast delegate only contains the reference of methods whose return type is void. The + and += operators are used to combine delegate instances. Multicast delegates are considered equal if they reference the same methods in the same order.

Example

 //1. Declaration
public delegate void MyDelagate(int a, int b); 
public class Example
{
 // methods to be assigned and called by delegate
 public void Sum(int a, int b)
 {
 Console.WriteLine("Sum of integers is = " + (a + b));
 }

 public void Difference(int a, int b)
 {
 Console.WriteLine("Difference of integer is = " + (a - b));
 }
}
class Program
{
 static void Main()
 {
 Example obj = new Example();
 // 2. Instantiation
 MyDelagate multicastdel = new MyDelagate(obj.Sum); 
 multicastdel += new MyDelagate(obj.Difference);
 
 // 3. Invocation
 multicastdel (50, 20);
 }
} 

Output

  Sum of integers is = 70 
 Difference of integer is = 30    
Conclusion

I hope you will enjoy the collections and collections interfaces while programming with C#. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome. Also, Consider our C# Programming Course for a better understanding of all C# concepts.

FAQs

It allows you to define and invoke methods dynamically.

 A reference type that can be used to encapsulate a named or an anonymous method

It allows methods to be passed as parameters

There are two types of delegates in C#, singlecast delegates and multiplecast delegates.

Take our Csharp skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this