in Technology by

What is a multicast delegate in C#?

1 Answer

0 votes
by

A multicast delegate holds the references or addresses to more than one function at a single time. Whenever we invoke the multicast delegate, it will invoke all the other functions that are being referred by that multicast delegate. You should use the complete method signature the same as the delegate to call multiple methods. For example:

namespace MulticastDelegate
{
public class Rectangle
{
public void Area(double Width, double Height)
{
Console.WriteLine(@"Area is {0}", (Width * Height));
}
public void Perimeter(double Width, double Height)
{
Console.WriteLine(@"Perimeter is {0}", (2 * (Width + Height)));
}
static void Main(string[] args)
{
Rectangle rect = new Rectangle();
rect.Area(23.45, 67.89);
rect.Perimeter(23.45, 67.89);
Console.ReadKey();
}
}
}

Here, we created an instance of the Rectangle class and then called the two different methods. Now a single delegate will invoke these two methods Area and Perimeter. These defined methods are having the same signature as the defined delegates that hold the reference to these methods.

Creating multicast delegate:

namespace MulticastDelegateDemo
{
public delegate void RectangleDelete(double Width, double Height);
public class Rectangle
{
public void Area(double Width, double Height)
{
Console.WriteLine(@"Area is {0}", (Width * Height));
}
public void Perimeter(double Width, double Height)
{
Console.WriteLine(@"Perimeter is {0}", (2 * (Width + Height)));
}
static void Main(string[] args)
{
Rectangle rect = new Rectangle();
RectangleDelete rectDelegate = new RectangleDelete(rect.Area);
rectDelegate += rect.Perimeter;
rectDelegate(23.45, 67.89);
Console.WriteLine();
rectDelegate.Invoke(13.45, 76.89);
Console.WriteLine();
//Removing a method from delegate object
rectDelegate -= rect.Perimeter;
rectDelegate.Invoke(13.45, 76.89);
Console.ReadKey();
}
}
}

Related questions

0 votes
0 votes
    What is a delegate in swift?...
asked Nov 30, 2020 in Technology by JackTerrance
+1 vote
    To delegate administrative tasks for specific resource groups in Azure which?...
asked Oct 20, 2020 in Technology by JackTerrance
0 votes
    I'm working on an extension (BHO) for Internet Explorer, and having trouble attaching a .NET delegate to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I want to assign to a datatable such that. If datatable is null create a new datatable else clear ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    I have developed one application in which i have to detect whether user has tapped home button or whether ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    I have developed one application in which i have to detect whether user has tapped home button or whether ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    What is Business Delegate pattern?...
asked Nov 7, 2020 in Education by Editorial Staff
0 votes
    Why do we use Async and Await in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What do you mean by value types and reference types in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    How you can implement nullable types in C#? explain with the syntax of Nullable type....
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is Garbage Collection in C#.Net?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is Reflection in C#.Net?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is the difference between method overloading and method overriding in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is the difference between dispose() and finalize() methods in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
...