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

Event declaration syntax in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event Mastery Badge
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 declaration and usage?

Consider the following C# code snippet. What will be printed when the program runs?

C Sharp (C#)
using System;

class Program {
    public delegate void Notify();
    public event Notify OnNotify;

    public void Trigger() {
        OnNotify?.Invoke();
    }

    static void Main() {
        Program p = new Program();
        p.OnNotify += () => Console.WriteLine("Event triggered!");
        p.Trigger();
    }
}
ARuntime error: NullReferenceException
BNo output
CCompile-time error: event must be declared inside a class
DEvent triggered!
Attempts:
2 left
💡 Hint

Check if the event is subscribed before invoking.

Predict Output
intermediate
2:00remaining
What error occurs with this event declaration?

What error will this code produce?

C Sharp (C#)
using System;

class Program {
    public event Action OnAction = () => {};

    static void Main() {
        Program p = new Program();
        p.OnAction();
    }
}
ARuntime error: NullReferenceException
BCompile-time error: Events cannot have initializers
CNo output, runs fine
DCompile-time error: Missing delegate declaration
Attempts:
2 left
💡 Hint

Events cannot be assigned directly with initial values.

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

Examine the code below. Why does it throw an exception at runtime?

C Sharp (C#)
using System;

class Program {
    public event Action OnEvent;

    public void Raise() {
        OnEvent();
    }

    static void Main() {
        Program p = new Program();
        p.Raise();
    }
}
ANullReferenceException because OnEvent has no subscribers
BCompile-time error: event cannot be invoked outside the declaring class
CNo output, runs fine
DInvalidOperationException because event is null
Attempts:
2 left
💡 Hint

Check if the event is null before invoking.

📝 Syntax
advanced
2:00remaining
Which option correctly declares an event with a custom delegate?

Which of the following is the correct way to declare an event using a custom delegate in C#?

A
delegate public void MyEventHandler(string message);
event MyEventHandler EventOccurred;
B
public event delegate void MyEventHandler(string message);
public EventOccurred MyEventHandler;
C
public delegate void MyEventHandler(string message);
public event MyEventHandler EventOccurred;
D
public void delegate MyEventHandler(string message);
public event EventOccurred MyEventHandler;
Attempts:
2 left
💡 Hint

Remember the order: delegate declaration first, then event declaration.

🚀 Application
expert
2:00remaining
How many subscribers does the event have after this code runs?

Given the following code, how many methods are subscribed to the OnChange event after execution?

C Sharp (C#)
using System;

class Program {
    public event Action OnChange;

    public void Subscribe() {
        OnChange += Handler1;
        OnChange += Handler2;
        OnChange -= Handler1;
        OnChange += Handler3;
    }

    void Handler1() { }
    void Handler2() { }
    void Handler3() { }

    static void Main() {
        Program p = new Program();
        p.Subscribe();
        Console.WriteLine(p.OnChange?.GetInvocationList().Length ?? 0);
    }
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Consider the += and -= operations on the event.