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

Custom event arguments in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Event Arguments 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 custom event argument example?

Consider the following C# code that defines a custom event argument and raises an event. What will be printed when the event is triggered?

C Sharp (C#)
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");
    }
}
ANo output
BReceived: MessageEventArgs
CHello World
DReceived: Hello World
Attempts:
2 left
💡 Hint

Look at how the event handler prints the Message property from the custom event args.

🧠 Conceptual
intermediate
1:30remaining
Which statement about custom event arguments is true?

Choose the correct statement about creating and using custom event argument classes in C#.

ACustom event argument classes can only contain public fields, not properties.
BCustom event argument classes must inherit from <code>EventArgs</code> to be used in events.
CEvents cannot use custom event argument classes; only <code>EventArgs</code> can be used.
DCustom event argument classes must be sealed and cannot be inherited.
Attempts:
2 left
💡 Hint

Think about the base class convention for event argument classes in .NET.

🔧 Debug
advanced
2:00remaining
What error does this code produce?

Examine the following code snippet. What error will occur when compiling or running it?

C Sharp (C#)
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);
    }
}
ACompilation error: event invocation syntax incorrect
BNo error, prints nothing
CNullReferenceException at runtime
DInvalidCastException at runtime
Attempts:
2 left
💡 Hint

Check if the event has any subscribers before invoking it.

📝 Syntax
advanced
2:00remaining
Which option correctly declares and raises a custom event with arguments?

Choose the correct code snippet that declares a custom event with custom event arguments and raises it safely.

A
public event EventHandler&lt;MyArgs&gt; MyEvent;

protected virtual void OnMyEvent(string msg) {
    MyEvent?.Invoke(this, new MyArgs(msg));
}
B
public event EventHandler&lt;MyArgs&gt; MyEvent;

protected virtual void OnMyEvent(string msg) {
    if (MyEvent != null) MyEvent(this, msg);
}
C
public event EventHandler&lt;MyArgs&gt; MyEvent;

protected virtual void OnMyEvent(string msg) {
    MyEvent(this, new MyArgs(msg));
}
D
public event EventHandler&lt;MyArgs&gt; MyEvent;

protected virtual void OnMyEvent(string msg) {
    MyEvent.Invoke(new MyArgs(msg));
}
Attempts:
2 left
💡 Hint

Look for safe invocation and correct argument types.

🚀 Application
expert
2:30remaining
How many items are in the event args list after this code runs?

Given the code below, how many items will be in the Numbers list inside NumbersEventArgs when the event is raised?

C Sharp (C#)
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();
    }
}
A3
B5
C2
D0
Attempts:
2 left
💡 Hint

Look at when the event is raised and how many numbers have been added at that moment.