Challenge - 5 Problems
Event Safety Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this event invocation code?
Consider the following C# code that raises an event safely. What will be printed when
OnDataReceived is called?C Sharp (C#)
using System; class Program { public event EventHandler DataReceived; public void OnDataReceived() { EventHandler handler = DataReceived; if (handler != null) { handler(this, EventArgs.Empty); } } static void Main() { Program p = new Program(); p.DataReceived += (s, e) => Console.WriteLine("Event triggered"); p.OnDataReceived(); } }
Attempts:
2 left
💡 Hint
Think about how the event is copied to a local variable before invocation.
✗ Incorrect
The event is copied to a local variable 'handler' to avoid race conditions. Since the event has one subscriber, invoking 'handler' calls the subscriber and prints 'Event triggered'.
❓ Predict Output
intermediate2:00remaining
What happens if you invoke an event without null check?
What will happen when the following code runs?
C Sharp (C#)
using System; class Program { public event EventHandler DataReceived; public void OnDataReceived() { DataReceived(this, EventArgs.Empty); } static void Main() { Program p = new Program(); p.OnDataReceived(); } }
Attempts:
2 left
💡 Hint
What happens if you call an event with no subscribers?
✗ Incorrect
Since no subscriber is attached, the event delegate is null. Invoking it directly causes a NullReferenceException.
🔧 Debug
advanced2:00remaining
Identify the thread-safety issue in this event raising code
What is the problem with this code when raising events in a multithreaded environment?
C Sharp (C#)
using System; class Program { public event EventHandler DataReceived; public void OnDataReceived() { if (DataReceived != null) { DataReceived(this, EventArgs.Empty); } } }
Attempts:
2 left
💡 Hint
Consider what happens if another thread unsubscribes between the null check and invocation.
✗ Incorrect
Between the null check and invocation, another thread might unsubscribe all handlers, making the event null and causing a NullReferenceException.
❓ Predict Output
advanced2:00remaining
What is the output of this event invocation with multiple subscribers?
Given the code below, what will be printed when
OnDataReceived is called?C Sharp (C#)
using System; class Program { public event EventHandler DataReceived; public void OnDataReceived() { EventHandler handler = DataReceived; handler?.Invoke(this, EventArgs.Empty); } static void Main() { Program p = new Program(); p.DataReceived += (s, e) => Console.WriteLine("First subscriber"); p.DataReceived += (s, e) => Console.WriteLine("Second subscriber"); p.OnDataReceived(); } }
Attempts:
2 left
💡 Hint
Events call subscribers in the order they were added.
✗ Incorrect
Events invoke all subscribers in the order they were added. Both subscribers print their messages in sequence.
❓ Predict Output
expert2:00remaining
What is the output and why? (Event with custom delegate and null propagation)
Analyze the code below. What will be the output when
RaiseEvent is called?C Sharp (C#)
using System; class Program { public delegate void CustomEventHandler(string message); public event CustomEventHandler? CustomEvent; public void RaiseEvent() { CustomEvent?.Invoke("Hello World"); } static void Main() { Program p = new Program(); CustomEventHandler handler = msg => Console.WriteLine($"Received: {msg}"); p.CustomEvent += handler; p.RaiseEvent(); p.CustomEvent -= handler; p.RaiseEvent(); } }
Attempts:
2 left
💡 Hint
Events with null propagation operator do not throw if no subscribers.
✗ Incorrect
The null propagation operator safely invokes the event only if it has subscribers. Unsubscribing the handler makes the event null, so the second call does nothing.