Contents
Introduction
In earlier post we have discussed about the Delegates, a delegate holds the reference of a method. As the name suggests, a Multicast delegate is a delegate that holds the reference of more than one method. The only requisite is that all such methods should have the same signature, i.e. the same return type and the same number and type of parameters. When we invoke the multicast delegate, then all the functions which are referenced by the delegate are going to be invoked. If we want to call multiple methods using a delegate, then all the method signature should be the same.
For example. Suppose we have class contain two methods “GetArea(double width, double height)” and “GetPerimeter(double width, double height)”. Please note that the signature and return type of both these methods is same, so we can call both these methods by a one delegate.
using System;
namespace MathOpearation
{
public class Rect
{
public void GetArea(double width, double height)
{
Console.WriteLine(@"Area is {0}", (width * height));
}
public void GetPerimeter(double width, double height)
{
Console.WriteLine(@"Perimeter is {0}", (2 * (width + height)));
}
}
}
Main class
using System;
using MathOpearation;
namespace Conversion
{
class Test
{
public delegate void RectDelegate(double width, double height);
static void Main()
{
var rect = new Rect();
var rectDelegate = new RectDelegate(rect.GetArea);
rectDelegate += rect.GetPerimeter;
rectDelegate(12.98, 30.21);
Console.ReadLine();
}
}
}
Notice, in above code we have added a multiple method to the same delegate, when we call our delegate, with one single call, both the methods are getting executed,
rectDelegate += rect.GetPerimeter;
to remove assigned delegate from multicast delegate we can use simple syntax mentioned below
rectDelegate -= rect.GetPerimeter;
Example
using System;
using MathOpearation;
namespace Conversion
{
class Test
{
public delegate void RectDelegate(double width, double height);
static void Main()
{
var rect = new Rect();
var rectDelegate = new RectDelegate(rect.GetArea);
rectDelegate += rect.GetPerimeter;
rectDelegate(12.98, 30.21);
rectDelegate.Invoke(13.45, 6.89);
rectDelegate -= rect.GetPerimeter;
rectDelegate.Invoke(13.45, 6.89);
Console.ReadLine();
}
}
}
Order of Execution
A multicast delegate invokes the methods in the invocation list, in which they are added. In above example context, delegate holds the reference of two methods i.e. “GetArea” and “GetPerimeter” So, both will be executed one after another.
For an example: Notice in below example we have changed sequence of assigning reference of the method so first it will execute “GetPerimeter” and then it will execute “GetArea”.
using System;
using MathOpearation;
namespace Conversion
{
class Test
{
public delegate void RectDelegate(double width, double height);
static void Main()
{
var rect = new Rect();
var rectDelegate = new RectDelegate(rect.GetPerimeter);
rectDelegate += rect. GetArea;
rectDelegate(12.98, 30.21);
Console.ReadLine();
}
}
}
Delegates with Return Type
In Multicast delegates, when we invoke a delegate, it will call all the methods bound with the delegate and this is the advantage of a Multicast delegate. But when delegates have return type and Multicast delegate holds the reference of multiple value returning methods, we will get the result of the last method or from last methods only.
using System;
namespace Conversion
{
class Program
{
public delegate int Operation(int a, int b);
static void Main()
{
var program = new Program();
var delegateOperation = new Operation(program.Add);
delegateOperation += program.Sub;
var result = delegateOperation(10, 5);
Console.WriteLine(result);
Console.ReadLine();
}
public int Add(int a, int b)
{
return a + b;
}
public int Sub(int a, int b)
{
return a - b;
}
}
}
In above example out put will be 5 as we have added last method in delegate is “Sub(int a, int b)“ so it will return as result from that method only.
This will be applicable when the function using ‘out’ or ’ref’ keyword. i.e. last function value will be return as result from the delegate
Delegate InvocationList
A Multicast Delegate has a linked list of delegates, called an invocation list. i.e. all delegate we assigned will be added in to list. So When a multicast delegate is invoked, the delegates in the invocation list are called synchronously in the order in which they appear.

Exception handling in Multicast Delegate
Suppose we have added multiple delegates to a single multicast delegate. Each of these individual delegates must be invoked, regardless of whether an unhandled exception is thrown within one of the delegates. But, once a delegate in a multicast delegate throws an unhandled exception, no more delegates are fired. we need a way to trap unhandled exceptions within each individual delegate while still allowing the rest of the delegates to fire.
To avoid breaking the chain, we have to gracefully handle exceptions in all functions. Use the GetInvocationList method. This method returns each individual delegate from a multicast delegate and, by doing so, allows us to invoke each delegate within the try block of an exception handler.
Example:
using System;
namespace Conversion
{
class Program
{
public delegate void RectDelegate(double width, double height);
static void Main()
{
var rect = new Rect();
var rectDelegate = new RectDelegate(rect.GetArea);
rectDelegate += rect.GetPerimeter;
var delegates = rectDelegate.GetInvocationList();
foreach (var deleg in delegates)
{
try
{
deleg.DynamicInvoke(12.89, 12.11);
}
catch (Exception e)
{
Console.WriteLine($"Exception in {deleg.Method.Name}");
}
}
Console.ReadLine();
}
}
public class Rect
{
public void GetArea(double width, double height)
{
Console.WriteLine(@"Area is {0}", (width * height));
}
public void GetPerimeter(double width, double height)
{
Console.WriteLine(@"Perimeter is in action");
}
}
}
Typically, multicast delegate used in publisher and subscriber pattern
Multicast Delegates free online Quiz to test the knowledge. Here is list of Multicast Delegates Interview Questions.