0
0
C Sharp (C#)programming~20 mins

Raising events safely in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event Safety Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
    }
}
AEvent triggered
BNo output
CNullReferenceException at runtime
DCompilation error due to event invocation
Attempts:
2 left
💡 Hint
Think about how the event is copied to a local variable before invocation.
Predict Output
intermediate
2: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();
    }
}
AThrows NullReferenceException at runtime
BPrints 'Event triggered'
CCompilation error due to missing event handler
DNo output and no error
Attempts:
2 left
💡 Hint
What happens if you call an event with no subscribers?
🔧 Debug
advanced
2: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);
        }
    }
}
AThe event is invoked twice accidentally
BThe event can become null after the null check but before invocation, causing NullReferenceException
CThe event handler list is modified during invocation causing InvalidOperationException
DNo thread-safety issues present
Attempts:
2 left
💡 Hint
Consider what happens if another thread unsubscribes between the null check and invocation.
Predict Output
advanced
2: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();
    }
}
AOnly First subscriber
BSecond subscriber\nFirst subscriber
CFirst subscriber\nSecond subscriber
DNo output
Attempts:
2 left
💡 Hint
Events call subscribers in the order they were added.
Predict Output
expert
2: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();
    }
}
ACompilation error due to null assignment to event
B
Received: Hello World
Throws NullReferenceException on second call
C
No output
Throws NullReferenceException on second call
D
Received: Hello World
(no output on second call)
Attempts:
2 left
💡 Hint
Events with null propagation operator do not throw if no subscribers.