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

EventHandler delegate pattern in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
EventHandler 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 subscription code?

Consider the following C# code using the EventHandler delegate pattern. What will be printed when the program runs?

C Sharp (C#)
using System;

class Publisher
{
    public event EventHandler? OnChange;

    public void RaiseEvent()
    {
        OnChange?.Invoke(this, EventArgs.Empty);
    }
}

class Program
{
    static void Main()
    {
        Publisher pub = new Publisher();
        pub.OnChange += Handler1;
        pub.OnChange += Handler2;
        pub.RaiseEvent();
    }

    static void Handler1(object? sender, EventArgs e)
    {
        Console.WriteLine("Handler1 called");
    }

    static void Handler2(object? sender, EventArgs e)
    {
        Console.WriteLine("Handler2 called");
    }
}
ANo output
BHandler2 called\nHandler1 called
CHandler1 called
DHandler1 called\nHandler2 called
Attempts:
2 left
💡 Hint

Events call their subscribed handlers in the order they were added.

Predict Output
intermediate
2:00remaining
What happens if no handlers are subscribed?

What is the output of this code when RaiseEvent is called but no handlers are subscribed?

C Sharp (C#)
using System;

class Publisher
{
    public event EventHandler? OnChange;

    public void RaiseEvent()
    {
        OnChange?.Invoke(this, EventArgs.Empty);
        Console.WriteLine("Event raised");
    }
}

class Program
{
    static void Main()
    {
        Publisher pub = new Publisher();
        pub.RaiseEvent();
    }
}
ANullReferenceException
BCompilation error
CEvent raised
DNo output
Attempts:
2 left
💡 Hint

The null-conditional operator ?. prevents errors if no handlers exist.

🔧 Debug
advanced
2:00remaining
Why does this event handler code cause a runtime error?

Examine this code. Why does it throw a NullReferenceException when RaiseEvent is called?

C Sharp (C#)
using System;

class Publisher
{
    public event EventHandler OnChange;

    public void RaiseEvent()
    {
        OnChange(this, EventArgs.Empty);
    }
}

class Program
{
    static void Main()
    {
        Publisher pub = new Publisher();
        pub.RaiseEvent();
    }
}
AThe event OnChange must be static to be invoked.
BThe event OnChange is null because no handlers are subscribed, so calling it causes NullReferenceException.
CThe event OnChange requires a custom delegate type, not EventHandler.
DThe RaiseEvent method must be async to call the event.
Attempts:
2 left
💡 Hint

Events are null if no handlers are attached. Calling a null delegate causes an error.

📝 Syntax
advanced
2:00remaining
Which option correctly declares an event using EventHandler?

Which of the following is the correct way to declare an event named OnChange using the EventHandler delegate?

Apublic event EventHandler OnChange;
Bpublic EventHandler OnChange();
Cpublic event void OnChange(EventHandler handler);
Dpublic delegate EventHandler OnChange;
Attempts:
2 left
💡 Hint

Events use the event keyword followed by the delegate type and event name.

🚀 Application
expert
3:00remaining
How many handlers are called and what is the output?

Given this code, how many handlers are called and what is the exact output?

C Sharp (C#)
using System;

class Publisher
{
    public event EventHandler? OnChange;

    public void RaiseEvent()
    {
        OnChange?.Invoke(this, EventArgs.Empty);
    }
}

class Program
{
    static void Main()
    {
        Publisher pub = new Publisher();
        pub.OnChange += Handler1;
        pub.OnChange += Handler2;
        pub.OnChange -= Handler1;
        pub.RaiseEvent();
    }

    static void Handler1(object? sender, EventArgs e)
    {
        Console.WriteLine("Handler1 called");
    }

    static void Handler2(object? sender, EventArgs e)
    {
        Console.WriteLine("Handler2 called");
    }
}
AOne handler called\nHandler2 called
BTwo handlers called\nHandler1 called\nHandler2 called
CNo handlers called\nNo output
DOne handler called\nHandler1 called
Attempts:
2 left
💡 Hint

Removing a handler unsubscribes it from the event.