C# Delegates Tutorial For Beginners With Examples

A Delegate is an object that can hold the address of a method.
A Delegate object can refer only those methods whose signature matches with the Delegate type.

Creating Delegate Types

Delegate types in C# can be created as below:
    public delegate void Add(int number1, int number2);   
        
In the above example Add is the Delegate type and a Delegate object of this type can refer any method that matches the signature of this Delegate type.

Initializing a Delegate

A Delegate object can be initialized by referring a method with a matching signature.
In the below example a method called as AddNumbers with matching signature is created. Then a Delegate object calculate of type Add is initialized with the address of method AddNumbers.
        
    public delegate void Add(int number1, int number2);

    private static void AddNumbers(int firstNumber, int secondNumber)
    {
        Console.WriteLine(firstNumber + secondNumber);
    }

    static void Main(string[] args)
    {
        //Delegate initialized
        Add calculate = AddNumbers;

        Console.ReadLine();
    }      
        
Delegates in C# are like function pointers used in C and C++.

Invoking Delegates

The Delegate calculate of the previous example can be invoked as below:
    calculate(1,2);
        
Output:
3
Since the Delegate calculate refers the method AddNumbers so invoking this Delegate executes the method AddNumbers.
The complete program is shown below:
    public delegate void Add(int number1, int number2);

    private static void AddNumbers(int firstNumber, int secondNumber)
    {
        Console.WriteLine(firstNumber + secondNumber);
    }

    static void Main(string[] args)
    {
        //Delegate initialized
        Add calculate = AddNumbers;

        calculate(1, 2);

        Console.ReadLine();
    }
        
Since the Delegate can hold the address of another method so a Delegate can be passed as a parameter.
In the below example the Delegate calculate which holds address of the method AddNumbers is passed as a parameter to another method TakeDelegate.
The TakeDelegate method just invokes the Delegate without caring for the method name that gets executed through the Delegate.
The below example shows how this is done:
            
    public delegate void Add(int number1, int number2);

    private static void AddNumbers(int firstNumber, int secondNumber)
    {
        Console.WriteLine(firstNumber + secondNumber);
    }

    private static void TakeDelegate(string msg, int n1, int n2, Add callMethod)
    {
        Console.WriteLine(msg);
        callMethod(n1, n2);
    }

    static void Main(string[] args)
    {
        //Delegate initialized
        Add calculate = AddNumbers;

        //Pass the address of the AddNumbers method using Delegate calculate 
        TakeDelegate("Invoking Delegate", 21, 33, calculate);            

        Console.ReadLine();
    }
            
Output:
Invoking Delegate
54

Multicast Delegates

A Delegate object which holds references to multiple Delegate objects is known as a Multicast Delegate.
A multicast Delegate object maintains a list of the referred Delegate objects as per the order by which they were added. When a multicast Delegate object is called the referred Delegate objects are invoked as per the order maintained in the list.
    public delegate void MultiShow();

    private static void ShowMsg1()
    {
        Console.WriteLine("Method 1");
    }

    private static void ShowMsg2()
    {
        Console.WriteLine("Method 2");
    }

    private static void ShowMsg3()
    {
        Console.WriteLine("Method 3");
    }

    static void Main(string[] args)
    {
        MultiShow multiDelegateMsg, delShowMsg1, delShowMsg2, delShowMsg3;

        delShowMsg1 = ShowMsg1;
        delShowMsg2 = ShowMsg2;
        delShowMsg3 = ShowMsg3;

        multiDelegateMsg = delShowMsg1 + delShowMsg2 + delShowMsg3;

        //Invoke Multicast delegate
        multiDelegateMsg();

        Console.ReadLine();
    }
           
Output:
Method 1
Method 2
Method 3