It is a design pattern where a publisher sends messages or events without knowing who will receive them, and subscribers listen for these messages to react accordingly.
Click to reveal answer
beginner
In C#, what keyword is commonly used to declare events in the Publisher-Subscriber model?
The event keyword is used to declare events that subscribers can listen to.
Click to reveal answer
beginner
How does a subscriber register to receive notifications from a publisher in C#?
A subscriber adds a method to the publisher's event using the += operator to subscribe.
Click to reveal answer
beginner
What happens when a publisher raises an event in the Publisher-Subscriber model?
All subscriber methods attached to that event are called automatically to handle the event.
Click to reveal answer
beginner
Why is the Publisher-Subscriber model useful in programming?
It helps decouple components, so publishers and subscribers don't need to know about each other directly, making code easier to maintain and extend.
Click to reveal answer
In the Publisher-Subscriber model, who knows about the subscribers?
ASubscribers know about each other
BOnly the publisher knows about the subscribers
CNeither publisher nor subscribers know about each other
DOnly the subscribers know about the publisher
✗ Incorrect
Subscribers know about the publisher because they subscribe to its events, but the publisher does not know the subscribers directly.
Which C# operator is used by subscribers to attach their methods to a publisher's event?
A+=
B-=
C==
D=>
✗ Incorrect
The '+=' operator is used to subscribe a method to an event.
What keyword declares an event in a C# publisher class?
Aevent
Bpublish
Csubscribe
Ddelegate
✗ Incorrect
The 'event' keyword declares an event that subscribers can listen to.
What is the main benefit of using the Publisher-Subscriber model?
ATight coupling between components
BFaster execution speed
CDecoupling components for easier maintenance
DDirect method calls between classes
✗ Incorrect
It decouples components, so they can work independently and be maintained easily.
When a publisher raises an event, what happens?
AOnly the first subscriber is notified
BAll subscribers attached to the event are notified
CNo subscribers are notified automatically
DSubscribers must poll the publisher
✗ Incorrect
All subscribers attached to the event are called automatically when the event is raised.
Explain how the Publisher-Subscriber model works in C# with an example of subscribing and raising an event.
Think about how events and delegates work together.
You got /4 concepts.
Describe the advantages of using the Publisher-Subscriber execution model in software design.
Consider how components communicate without tight connections.
You got /4 concepts.
Practice
(1/5)
1. In the publisher-subscriber model in C#, what is the main role of the subscriber?
easy
A. To send messages to multiple listeners
B. To register methods that respond to events from the publisher
C. To create new events for the publisher
D. To directly modify the publisher's data
Solution
Step 1: Understand the roles in the model
The publisher sends messages or events, while the subscriber listens and reacts.
Step 2: Identify subscriber's responsibility
Subscribers register their methods to handle events raised by the publisher.
Final Answer:
To register methods that respond to events from the publisher -> Option B
Quick Check:
Subscriber = method registration [OK]
Hint: Subscribers listen and react by registering methods [OK]
Common Mistakes:
Confusing publisher and subscriber roles
Thinking subscriber sends messages
Assuming subscriber modifies publisher directly
2. Which of the following is the correct syntax to subscribe a method named OnDataReceived to an event DataReceived in C#?
easy
A. DataReceived += OnDataReceived;
B. DataReceived = OnDataReceived();
C. DataReceived -= OnDataReceived;
D. OnDataReceived += DataReceived;
Solution
Step 1: Recall event subscription syntax
In C#, subscribing to an event uses the += operator with the method name (without parentheses).
Step 2: Match correct syntax
DataReceived += OnDataReceived; correctly subscribes the method.
Final Answer:
DataReceived += OnDataReceived; -> Option A
Quick Check:
Subscribe event = += method [OK]
Hint: Use += to subscribe methods to events [OK]
Common Mistakes:
Using = instead of +=
Calling method with parentheses when subscribing
Confusing subscription with unsubscription (-=)
3. Consider the following C# code snippet:
public class Publisher {
public event Action Notify;
public void Raise() {
Notify?.Invoke();
}
}
public class Subscriber {
public void Respond() {
Console.WriteLine("Event received");
}
}
var pub = new Publisher();
var sub = new Subscriber();
pub.Notify += sub.Respond;
pub.Raise();
What will be the output when pub.Raise() is called?
medium
A. No output
B. NullReferenceException
C. Event received
D. Compile-time error
Solution
Step 1: Understand event subscription and invocation
The subscriber's Respond method is subscribed to the Notify event, so it will be called when Raise() invokes Notify.
Step 2: Analyze output of Respond method
Respond prints "Event received" to the console, so this text will appear.
Final Answer:
Event received -> Option C
Quick Check:
Subscribed method runs and prints message [OK]
Hint: Subscribed methods run on event invoke [OK]
Common Mistakes:
Forgetting to subscribe the method
Expecting no output if event is null
Confusing event invocation syntax
4. The following code tries to unsubscribe a method from an event but causes a runtime error:
public class Publisher {
public event Action Notify;
}
var pub = new Publisher();
pub.Notify -= () => Console.WriteLine("Hello");
What is the main issue causing the error?
medium
A. Lambda expressions cannot be used with events
B. Events cannot be unsubscribed in C#
C. The event Notify is not initialized
D. Cannot unsubscribe a lambda expression that was never subscribed
Solution
Step 1: Understand event unsubscription rules
In C#, events start as null delegates. Unsubscribing (-=) from a null event throws NullReferenceException.
Step 2: Identify cause of runtime error
No prior subscription means Notify is null, so pub.Notify -= lambda throws NullReferenceException. The lambda was never subscribed.
Final Answer:
Cannot unsubscribe a lambda expression that was never subscribed -> Option D
Quick Check:
Null event -= throws NullRef [OK]
Hint: Unsubscribe only delegates you subscribed before [OK]
Common Mistakes:
Assuming unsubscribing any lambda works
Thinking events cannot be unsubscribed
Confusing null event invocation with unsubscription
5. You want to implement a publisher-subscriber system where multiple subscribers can register to receive messages, but some subscribers should only receive messages if a condition is true (e.g., message contains a keyword). Which approach best applies this conditional filtering in C#?
hard
A. Use event subscription with wrapper methods that check the condition before handling
B. Modify the publisher to send messages only to specific subscribers directly
C. Use multiple events for each condition and subscribe accordingly
D. Use a single event and let subscribers unsubscribe if condition is false
Solution
Step 1: Understand conditional filtering in subscriber model
The publisher sends events to all subscribers; filtering is best done in subscriber methods.
Step 2: Apply wrapper methods for condition
Subscribers can register methods that first check the condition and only proceed if true, keeping publisher simple.
Step 3: Evaluate other options
Modifying publisher to target subscribers breaks decoupling; multiple events add complexity; unsubscribing dynamically is inefficient.
Final Answer:
Use event subscription with wrapper methods that check the condition before handling -> Option A