Complete the code to declare an event using the EventHandler delegate.
public class Button { public event [1] Click; }
The EventHandler delegate is used to declare events that follow the standard pattern with sender and EventArgs parameters.
Complete the code to raise the Click event safely.
protected virtual void OnClick() {
[1]?.Invoke(this, EventArgs.Empty);
}The event named Click is invoked safely using the null-conditional operator to check if there are subscribers.
Fix the error in the event subscription code.
Button btn = new Button(); btn.Click += [1]; void Btn_Click(object sender, EventArgs e) { Console.WriteLine("Button clicked"); }
When subscribing to an event, use the method name without parentheses or parameters.
Fill both blanks to declare and raise a custom event using EventHandler.
public class Alarm { public event [1] AlarmRaised; public void RaiseAlarm() { AlarmRaised[2].Invoke(this, EventArgs.Empty); } }
The event is declared with EventHandler and raised safely using the null-conditional operator ?.
Fill all three blanks to create a dictionary of event handlers and invoke one safely.
var handlers = new Dictionary<string, [1]>(); handlers["start"] = (sender, e) => Console.WriteLine("Started"); if (handlers.TryGetValue("start", out var handler)) { handler[2].Invoke([3], EventArgs.Empty); }
The dictionary stores EventHandler delegates. The handler is invoked safely using the null-conditional operator, and null is passed as sender.