Consider the following C# code using the EventHandler delegate pattern. What will be printed when the program runs?
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"); } }
Events call their subscribed handlers in the order they were added.
The event OnChange has two handlers added: Handler1 then Handler2. When RaiseEvent is called, both handlers run in order, printing their messages.
What is the output of this code when RaiseEvent is called but no handlers are subscribed?
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(); } }
The null-conditional operator ?. prevents errors if no handlers exist.
The event OnChange is null because no handlers are subscribed. The ?.Invoke safely skips calling handlers. Then the message "Event raised" is printed.
Examine this code. Why does it throw a NullReferenceException when RaiseEvent is called?
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(); } }
Events are null if no handlers are attached. Calling a null delegate causes an error.
Because no handlers are subscribed, OnChange is null. Calling OnChange(this, EventArgs.Empty) without checking causes a NullReferenceException. Using OnChange?.Invoke(...) avoids this.
Which of the following is the correct way to declare an event named OnChange using the EventHandler delegate?
Events use the event keyword followed by the delegate type and event name.
Option A correctly declares an event named OnChange of type EventHandler. Other options are invalid syntax for events.
Given this code, how many handlers are called and what is the exact output?
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"); } }
Removing a handler unsubscribes it from the event.
Initially, both Handler1 and Handler2 are subscribed. Then Handler1 is removed. So only Handler2 runs, printing "Handler2 called" once.