Introduction
Multicast delegates let you call multiple methods with one delegate. It helps when you want to do many things in order with a single call.
delegate void MyDelegate();
MyDelegate d = Method1;
d += Method2;
d();delegate void Notify(); void Alert() { Console.WriteLine("Alert!"); } void Log() { Console.WriteLine("Log entry"); } Notify notify = Alert; notify += Log; notify();
delegate void ShowMessage(string msg); void Hello(string msg) { Console.WriteLine($"Hello {msg}"); } void Bye(string msg) { Console.WriteLine($"Bye {msg}"); } ShowMessage show = Hello; show += Bye; show("Friend");
using System; class Program { delegate void MultiDelegate(); static void First() { Console.WriteLine("First method called"); } static void Second() { Console.WriteLine("Second method called"); } static void Main() { MultiDelegate multi = First; multi += Second; multi(); multi -= First; Console.WriteLine("After removing First method:"); multi(); } }
ShowMessage to a multicast delegate del?delegate void Notify();
class Program {
static void A() => Console.WriteLine("A called");
static void B() => Console.WriteLine("B called");
static void Main() {
Notify del = A;
del += B;
del();
}
}delegate void Notify();
class Program {
static void A() => Console.WriteLine("A called");
static void Main() {
Notify del = A;
del -= A;
del();
}
}LogStart, ProcessData, and LogEnd. Which code correctly sets up and invokes this delegate?Pass the test and this pattern moves unknown