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

Why events are needed in C Sharp (C#) - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
2:00remaining
Why use events in C#?

Why are events important in C# programming?

AThey make the program run faster by using multiple threads automatically.
BThey allow objects to notify other objects when something happens, enabling loose coupling.
CThey replace all methods and properties in a class.
DThey are used to store data persistently on disk.
Attempts:
2 left
💡 Hint

Think about how one part of a program can tell another part that something happened without knowing details.

Predict Output
intermediate
2:00remaining
Output of event subscription example

What is the output of this C# code?

C Sharp (C#)
using System;

class Publisher {
    public event Action OnChange;
    public void Raise() {
        OnChange?.Invoke();
    }
}

class Program {
    static void Main() {
        Publisher p = new Publisher();
        p.OnChange += () => Console.WriteLine("Event triggered");
        p.Raise();
    }
}
AEvent triggered
BNo output
CCompile error: event not assigned
DRuntime error: null reference
Attempts:
2 left
💡 Hint

Look at how the event is subscribed and then invoked.

🔧 Debug
advanced
2:00remaining
Identify the error in event usage

What error will this code produce?

C Sharp (C#)
using System;

class Publisher {
    public event Action OnChange;
    public void Raise() {
        OnChange();
    }
}

class Program {
    static void Main() {
        Publisher p = new Publisher();
        p.Raise();
    }
}
ACompile-time error: event cannot be invoked
BNo error, prints nothing
CNullReferenceException at runtime
DStackOverflowException due to recursion
Attempts:
2 left
💡 Hint

What happens if you call an event with no subscribers?

📝 Syntax
advanced
2:00remaining
Correct syntax to declare and raise an event

Which option shows the correct way to declare and raise an event in C#?

Apublic event Action OnChange; public void Raise() { OnChange?.Invoke(); }
Bpublic delegate void OnChange(); public event OnChange Raise();
Cevent public Action OnChange; void Raise() { OnChange.Invoke(); }
Dpublic Action event OnChange; void Raise() { OnChange(); }
Attempts:
2 left
💡 Hint

Remember the order: 'public event' then type then name.

🚀 Application
expert
3:00remaining
How events improve program design

Which statement best explains how events improve program design?

AEvents replace the need for classes and objects in a program.
BEvents force all code to run sequentially, preventing bugs from parallel execution.
CEvents automatically save program state to a database for recovery.
DEvents allow components to communicate without tight connections, making code easier to change and extend.
Attempts:
2 left
💡 Hint

Think about how events help parts of a program work together without knowing too much about each other.