Recall & Review
beginner
What is the basic syntax to declare an event in C#?
An event is declared using the
event keyword followed by a delegate type and the event name, like: <br>public event EventHandler MyEvent;Click to reveal answer
beginner
What delegate type is commonly used for events in C#?
The
EventHandler delegate is commonly used for events. It has a signature of void(object sender, EventArgs e).Click to reveal answer
intermediate
Can you declare an event with a custom delegate type?
Yes, you can declare an event using any delegate type that matches the desired method signature. For example: <br>
public delegate void MyDelegate(string message);<br>public event MyDelegate MyEvent;Click to reveal answer
beginner
What access modifiers can be used with event declarations?
Events can have access modifiers like
public, private, protected, or internal to control their visibility.Click to reveal answer
intermediate
How do you raise an event inside a class?
You raise an event by calling it like a method, usually after checking if it is not null: <br>
MyEvent?.Invoke(this, EventArgs.Empty);Click to reveal answer
Which keyword is used to declare an event in C#?
✗ Incorrect
The
event keyword is used to declare events in C#.What is the default signature of the EventHandler delegate?
✗ Incorrect
EventHandler has the signature
void(object sender, EventArgs e).How do you check if an event has subscribers before raising it?
✗ Incorrect
You check if the event is not null before invoking it to avoid exceptions.
Can you declare an event with a custom delegate type?
✗ Incorrect
Events can use any delegate type matching the signature you want.
Which of these is a valid event declaration?
✗ Incorrect
The correct syntax is
public event EventHandler DataReceived;.Explain how to declare and raise an event in C# using the EventHandler delegate.
Think about the syntax for declaration and how to safely call the event.
You got /5 concepts.
Describe how you can use a custom delegate type for an event and why you might want to do that.
Consider how different event data might require different delegate signatures.
You got /4 concepts.