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 Rectangle {
  
   public delegate void RectDelegate(double Width, double Height);
    public void Area(double Width, double Height)
    {
        Console.WriteLine($"Area of rectangle is {Width * Height}.");
    }
    public void Perimeter(double Width, double Height)
    {
        Console.WriteLine($"Perimeter of rectangle is {2 * (Width + Height)}.");
    }
    static void Main(string[] args)
    {
        Rectangle rect = new Rectangle();
        RectDelegate obj = rect.Area;
        obj += rect.Perimeter;
        // We can call area and perimeter by invoking a single delegate.
        obj.Invoke(11.27, 35.75);
    }
}
}

Output

In the above-mentioned illustration, we first created a 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.

Multicast Delegates in C#

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.

Similar Reads

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....

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....

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....

Contact Us