Consider the following C# code that defines a custom event argument and raises an event. What will be printed when the event is triggered?
using System; public class MessageEventArgs : EventArgs { public string Message { get; } public MessageEventArgs(string message) => Message = message; } public class Publisher { public event EventHandler<MessageEventArgs>? MessageSent; public void SendMessage(string msg) { MessageSent?.Invoke(this, new MessageEventArgs(msg)); } } public class Subscriber { public void OnMessageReceived(object? sender, MessageEventArgs e) { Console.WriteLine($"Received: {e.Message}"); } } class Program { static void Main() { var pub = new Publisher(); var sub = new Subscriber(); pub.MessageSent += sub.OnMessageReceived; pub.SendMessage("Hello World"); } }
Look at how the event handler prints the Message property from the custom event args.
The event MessageSent is raised with a MessageEventArgs containing the string "Hello World". The subscriber prints "Received: " followed by the Message property, so the output is "Received: Hello World".
Choose the correct statement about creating and using custom event argument classes in C#.
Think about the base class convention for event argument classes in .NET.
In C#, custom event argument classes should inherit from EventArgs to follow the standard pattern and allow compatibility with event handlers.
Examine the following code snippet. What error will occur when compiling or running it?
using System; public class DataEventArgs : EventArgs { public int Data { get; set; } } public class Emitter { public event EventHandler<DataEventArgs> DataChanged; public void ChangeData(int value) { DataChanged(this, new DataEventArgs { Data = value }); } } class Program { static void Main() { var emitter = new Emitter(); emitter.ChangeData(10); } }
Check if the event has any subscribers before invoking it.
The event DataChanged is never subscribed to, so it is null. Invoking it without checking causes a NullReferenceException at runtime.
Choose the correct code snippet that declares a custom event with custom event arguments and raises it safely.
Look for safe invocation and correct argument types.
Option A uses the null-conditional operator to safely invoke the event and passes the correct arguments: sender and custom event args.
Given the code below, how many items will be in the Numbers list inside NumbersEventArgs when the event is raised?
using System; using System.Collections.Generic; public class NumbersEventArgs : EventArgs { public List<int> Numbers { get; } public NumbersEventArgs(List<int> numbers) => Numbers = numbers; } public class NumberGenerator { public event EventHandler<NumbersEventArgs>? NumbersGenerated; public void Generate() { var nums = new List<int>(); for (int i = 0; i < 5; i++) { nums.Add(i); if (i == 2) NumbersGenerated?.Invoke(this, new NumbersEventArgs(new List<int>(nums))); } } } class Program { static void Main() { var gen = new NumberGenerator(); gen.NumbersGenerated += (s, e) => Console.WriteLine(e.Numbers.Count); gen.Generate(); } }
Look at when the event is raised and how many numbers have been added at that moment.
The event is raised when i == 2. At that point, numbers 0, 1, and 2 have been added, so the list has 3 items.