3. What will be the output of the following C# code?
using System;
class Program {
public static event Action OnNotify;
static void Main() {
OnNotify += () => Console.WriteLine("Event triggered");
OnNotify?.Invoke();
}
}
medium
A. Event triggered
B. No output
C. Compilation error
D. Runtime exception
Solution
Step 1: Understand event subscription and invocation
The event OnNotify is subscribed with a lambda that prints "Event triggered". The event is then invoked safely with ?.Invoke().
Step 2: Predict output
Since OnNotify has a subscriber, invoking it calls the lambda, printing "Event triggered" to the console.
Final Answer:
Event triggered -> Option A
Quick Check:
Subscribed event invoked prints message = A [OK]
Hint: Subscribed event invocation prints output [OK]
Common Mistakes:
Ignoring null-conditional operator ?.Invoke()
Expecting no output without subscription
Confusing event with method call syntax
4. Identify the error in this C# event-driven code snippet:
public class Publisher {
public event EventHandler OnChange;
public void Raise() {
OnChange();
}
}
medium
A. OnChange event is invoked without checking for null
B. EventHandler delegate is not declared
C. Raise method should be static
D. Event should be private
Solution
Step 1: Review event invocation rules
Events can be null if no subscribers exist. Invoking without null check causes runtime error.
Step 2: Identify the error in code
OnChange() is called directly without checking if OnChange is null, risking NullReferenceException.
Final Answer:
OnChange event is invoked without checking for null -> Option A
Quick Check:
Invoke event safely with null check = C [OK]
Hint: Always check event for null before invoking [OK]
Common Mistakes:
Calling event without null check
Assuming event is never null
Misunderstanding event access modifiers
5. You want to design a system where multiple subscribers react differently to a button click event. Which approach best uses the event-driven pattern in C# to achieve this?
hard
A. Make subscribers poll the button state continuously in a loop
B. Create a list of methods and call them manually without events
C. Use a static method to handle all button clicks centrally
D. Declare a public event, let subscribers add their handlers, and invoke event on button click
Solution
Step 1: Understand event-driven design for multiple subscribers
Events allow multiple subscribers to register handlers that get called automatically when the event fires.
Step 2: Evaluate options for best practice
Declare a public event, let subscribers add their handlers, and invoke event on button click uses a public event and lets subscribers add handlers, which is the standard event-driven approach. Others are less efficient or not event-driven.
Final Answer:
Declare a public event, let subscribers add their handlers, and invoke event on button click -> Option D
Quick Check:
Use events for multiple subscriber reactions = A [OK]
Hint: Use events to notify multiple subscribers automatically [OK]
Common Mistakes:
Manually calling subscriber methods instead of using events
Using polling instead of event notifications
Centralizing all logic in static methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.