Monday 20 August 2018

C#: Multi cast delegate

What is a multi-cast delegate?
A delegate that points to more than one function is called multi cast delegate.

How to create a multi cast delegate?
There are two approaches to define multi-cast delegate.
a.   By chaining multiple instances of delegates (Using + operator)
b.   By registering multiple methods to same delegate (Using += operator)

Approach1: Multi cast delegate using + operator
By chaining multiple delegates using ‘+’ operator you can create a multi cast delegate.

Following statements define three delegates.

Process delegate1 = new Process(square);
Process delegate2 = new Process(cube);
Process delegate3 = new Process(twice);

By using ‘+’ operator we can chain all the above three delegates.

Process delegate4 = delegate1 + delegate2 + delegate3;

When you call delegate4, all the three delegates are executed in order.

Program.cs
using System;

class Program
{
    public delegate void Process(int num);

    public static void square(int num)
    {
        Console.WriteLine("Square of a number is {0}", num * num);
    }

    public static void cube(int num)
    {
        Console.WriteLine("Cube of a number is {0}", num * num * num);
    }

    public static void twice(int num)
    {
        Console.WriteLine("Twice of a number is {0}", 2 * num);
    }
   
   
    static void Main(string[] args)
    {
        Process delegate1 = new Process(square);
        Process delegate2 = new Process(cube);
        Process delegate3 = new Process(twice);

        Process delegate4 = delegate1 + delegate2 + delegate3;

        delegate4(10);
    }
}

Output
Square of a number is 100
Cube of a number is 1000
Twice of a number is 20

How to unchain a delegate?
By using ‘-‘ operator, you can unchain a delegate.

Example
delegate4 = delegate4 - delegate3;

Program.cs
using System;

class Program
{
    public delegate void Process(int num);

    public static void square(int num)
    {
        Console.WriteLine("Square of a number is {0}", num * num);
    }

    public static void cube(int num)
    {
        Console.WriteLine("Cube of a number is {0}", num * num * num);
    }

    public static void twice(int num)
    {
        Console.WriteLine("Twice of a number is {0}", 2 * num);
    }
   
   
    static void Main(string[] args)
    {
        Process delegate1 = new Process(square);
        Process delegate2 = new Process(cube);
        Process delegate3 = new Process(twice);

        Process delegate4 = delegate1 + delegate2 + delegate3;

        delegate4(10);

        Console.WriteLine("\nUnchaining twice method from multi cast delegate\n");

        delegate4 = delegate4 - delegate3;

        delegate4(10);
    }
}


Output
Square of a number is 100
Cube of a number is 1000
Twice of a number is 20

Unchaining twice method from multi cast delegate

Square of a number is 100
Cube of a number is 1000

Approach2: Multi cast delegate using += operator
By using ‘+=’ operator, you can add more than one method to same delegate instance.

Following statements add the methods square, cube and twice to myDelegate.

Process myDelegate = new Process(square);
myDelegate += cube;
myDelegate += twice;


Program.cs
using System;

class Program
{
    public delegate void Process(int num);

    public static void square(int num)
    {
        Console.WriteLine("Square of a number is {0}", num * num);
    }

    public static void cube(int num)
    {
        Console.WriteLine("Cube of a number is {0}", num * num * num);
    }

    public static void twice(int num)
    {
        Console.WriteLine("Twice of a number is {0}", 2 * num);
    }
   
   
    static void Main(string[] args)
    {
        Process myDelegate = new Process(square);
        myDelegate += cube;
        myDelegate += twice;

        myDelegate(10);
    }
}

Output
Square of a number is 100
Cube of a number is 1000
Twice of a number is 20


How to unchain methods from delegate?
By using ‘-=’ operator, you can unchain a method from delegate.

Following statement unchain the method ‘twice’ from myDelegate.

myDelegate -= twice;

Following is the complete working application.


Program.cs
using System;

class Program
{
    public delegate void Process(int num);

    public static void square(int num)
    {
        Console.WriteLine("Square of a number is {0}", num * num);
    }

    public static void cube(int num)
    {
        Console.WriteLine("Cube of a number is {0}", num * num * num);
    }

    public static void twice(int num)
    {
        Console.WriteLine("Twice of a number is {0}", 2 * num);
    }
   
   
    static void Main(string[] args)
    {
        Process myDelegate = new Process(square);
        myDelegate += cube;
        myDelegate += twice;

        myDelegate(10);

        Console.WriteLine("\nUnchaining twice method from multi cast delegate\n");
        myDelegate -= twice;

        myDelegate(10);
    }
}

Output
Square of a number is 100
Cube of a number is 1000
Twice of a number is 20

Unchaining twice method from multi cast delegate

Square of a number is 100
Cube of a number is 1000


Previous                                                 Next                                                 Home

No comments:

Post a Comment