Skip to content
Related Articles
Open in App
Not now

Related Articles

Multicast Delegates in C#

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 22 Nov, 2022
Improve Article
Save Article

A type-safe function pointer is a delegate. It means that the delegate contains a reference to a method or function, and that when we invoke the delegate, the method to which it refers will be executed. The delegate signature and the method to which it points must both be signed. The method must be passed as a parameter to the delegate function Object() { [native code] } when creating the delegate instance.

In C#, there are two kinds of Delegates. These are:

  • SingleCast Delegate: A single function or method is referred to as a delegate.
  • MultiCast Delegate: Delegate refers to the delegation of multiple functions or methods.

Multicast Delegate in C#:

One method reference, which is encapsulated in the delegate, is the only one that it is possible for the delegate to call. These Delegates are known as Multicast Delegates because they have the capacity to hold and invoke multiple methods. 

Multicast Delegates, also referred to as Combinable Delegates, are required to meet requirements such as the Delegate’s return type having to be void. No Delegate type parameter that is declared as an output parameter using no keywords is a Delegate type parameter. Concatenating the invocation lists of the two operands of the addition operation yields a Multicast Delegate instance by joining two Delegates. Delegates are used in the order that they are added.

In C#, a delegate that manages the references to multiple handler functions is known as a multicast delegate. All of the functions that the multicast delegate references will be called when the delegate is called. All method signatures should match if you want to use a delegate to call multiple methods.

Key factors relating to Multicast delegate:

  • Every method is called using the FIFO (First in, First out) principle.
  • Methods are added to delegates using the + or += operator.
  • Methods can be eliminated from the delegates list using the – or -= operator.

Example 1:

C#




// Multicast delegate example in C#
using System;
  
namespace MultDel
{
    public class GFG
    {
        public void Area(double Width, double Height)
        {
            Console.WriteLine($"Area is {Width * Height}");
        }
        public void Perimeter(double Width, double Height)
        {
            Console.WriteLine($"Perimeter is {2 * (Width + Height)}");
        }
        static void Main(string[] args)
        {
            GFG dim = new GFG();
            dim.Area(11.27, 35.75);
            dim.Perimeter(13.25, 45.66);
            Console.ReadKey();
        }
    }
}

Output:

 

In the above-mentioned illustration, we first created a GFG class instance before calling the two methods. Now I want to create a single delegate that will call the two methods mentioned above (i.e. Area and Perimeter). Due to the fact that the two methods have the same signature but different method names, we can create a single delegate that contains the references to both of the above-mentioned methods. The two methods mentioned above will be called when we invoke the delegate. And when we do that, C# refers to it as a Multicast Delegate.

Conclusion:

We can infer from the information above that a multicast delegate in C# is a delegate that controls references to multiple handler functions. When the multicast delegate is called, all of the functions it references are all called as well. If you want to use a delegate to call multiple methods, all of the method signatures must be identical.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!