📖 Scenario: Imagine you are building a simple notification system where multiple actions happen when an event occurs, like sending an email and logging a message.
🎯 Goal: You will create a multicast delegate that calls multiple methods when invoked, demonstrating how multicast delegates work in C#.
📋 What You'll Learn
Create a delegate type named Notify that takes no parameters and returns void
Create two methods named SendEmail and LogMessage that print specific messages
Create a multicast delegate instance combining SendEmail and LogMessage
Invoke the multicast delegate to see both messages printed
💡 Why This Matters
🌍 Real World
Multicast delegates are useful in event handling systems where multiple actions need to respond to a single event, such as UI updates, logging, and notifications.
💼 Career
Understanding multicast delegates helps in building responsive and modular C# applications, especially in desktop, web, and game development.
Progress0 / 4 steps
1
Create the delegate type and methods
Create a delegate type called Notify that takes no parameters and returns void. Then create two methods: SendEmail that prints "Email sent." and LogMessage that prints "Message logged."
C Sharp (C#)
Hint
Use delegate void Notify() to declare the delegate type. Methods must match this signature.
2
Create a multicast delegate instance
Inside the Main method, create a variable called notifyHandlers of type Notify and assign it to SendEmail. Then add LogMessage to notifyHandlers using the += operator.
C Sharp (C#)
Hint
Use Notify notifyHandlers = SendEmail; and then notifyHandlers += LogMessage; to combine methods.
3
Invoke the multicast delegate
Still inside the Main method, invoke the notifyHandlers delegate to call both SendEmail and LogMessage methods.
C Sharp (C#)
Hint
Call the delegate like a method: notifyHandlers();
4
Display the output
Run the program and observe the output. It should print both "Email sent." and "Message logged." lines.
C Sharp (C#)
Hint
Both messages should appear one after the other when you run the program.
Practice
(1/5)
1. What is the main purpose of a multicast delegate in C#?
easy
A. To call multiple methods with a single delegate invocation
B. To store only one method reference at a time
C. To create a delegate that returns multiple values
D. To prevent methods from being called more than once
Solution
Step 1: Understand delegate basics
A delegate holds references to methods and can invoke them.
Step 2: Identify multicast delegate behavior
Multicast delegates can hold multiple method references and call them all in order.
Final Answer:
To call multiple methods with a single delegate invocation -> Option A
Quick Check:
Multicast delegates call many methods [OK]
Hint: Multicast delegates call many methods at once [OK]
2. Which syntax correctly adds a method named ShowMessage to a multicast delegate del?
easy
A. del += ShowMessage;
B. del -+ ShowMessage;
C. del =+ ShowMessage;
D. del = ShowMessage;
Solution
Step 1: Recall delegate addition syntax
To add a method to a multicast delegate, use the '+=' operator.
Step 2: Check each option
del += ShowMessage; uses 'del += ShowMessage;' which is correct syntax to add a method.
Final Answer:
del += ShowMessage; -> Option A
Quick Check:
Use '+=' to add methods to multicast delegates [OK]
Hint: Use '+=' to add methods to multicast delegates [OK]
Common Mistakes:
Using '=' instead of '+=' to add methods
Typing operators incorrectly like '=+' or '-+'
Confusing removal syntax with addition
3. What will be the output of the following code?
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();
}
}
medium
A. No output
B. B called
A called
C. A called
D. A called
B called
Solution
Step 1: Understand delegate invocation order
Methods added to a multicast delegate are called in the order they were added.
Step 2: Trace the code execution
First, method A is assigned, then method B is added. Calling del() invokes A then B.
Final Answer:
A called
B called -> Option D
Quick Check:
Multicast delegates call methods in added order [OK]
Hint: Methods run in order added to delegate [OK]
Common Mistakes:
Assuming reverse order of method calls
Expecting only the last method to run
Thinking no output occurs without explicit loop
4. Identify the error in this code snippet:
delegate void Notify();
class Program {
static void A() => Console.WriteLine("A called");
static void Main() {
Notify del = A;
del -= A;
del();
}
}
medium
A. Syntax error in delegate declaration
B. del is null when invoked, causing a runtime error
C. Cannot remove method from delegate
D. No error, output is 'A called'
Solution
Step 1: Analyze delegate removal
Removing the only method from the delegate leaves it null.
Step 2: Check invocation after removal
Calling del() when del is null causes a NullReferenceException at runtime.
Final Answer:
del is null when invoked, causing a runtime error -> Option B
Quick Check:
Removing last method makes delegate null [OK]
Hint: Removing last method makes delegate null, check before calling [OK]
Common Mistakes:
Assuming delegate still holds method after removal
Ignoring null check before invoking delegate
Thinking removal causes syntax error
5. You want to create a multicast delegate that calls three methods: LogStart, ProcessData, and LogEnd. Which code correctly sets up and invokes this delegate?
hard
A. Notify del; del = LogStart; del = ProcessData; del = LogEnd; del();
B. Notify del = LogStart + ProcessData + LogEnd; del();
C. Notify del = LogStart; del += ProcessData; del += LogEnd; del();
D. Notify del = LogStart; del -= ProcessData; del += LogEnd; del();
Solution
Step 1: Understand delegate combination
Use '+=' to add methods to a multicast delegate one by one.
Step 2: Evaluate each option
Notify del = LogStart; del += ProcessData; del += LogEnd; del(); correctly initializes with LogStart, then adds ProcessData and LogEnd, then calls del().
Final Answer:
Notify del = LogStart; del += ProcessData; del += LogEnd; del(); -> Option C
Quick Check:
Use '+=' repeatedly to build multicast delegate [OK]
Hint: Add methods one by one with '+=' to build multicast delegate [OK]
Common Mistakes:
Using '+' operator directly without delegate type
Overwriting delegate instead of adding methods
Removing methods unintentionally before calling
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.