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

Event declaration syntax in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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#?
Ainterface
Bevent
Cclass
Ddelegate
What is the default signature of the EventHandler delegate?
Avoid(object sender, EventArgs e)
Bint(object sender, EventArgs e)
Cvoid()
Dbool(object sender, EventArgs e)
How do you check if an event has subscribers before raising it?
ACheck if the event is not null
BCheck if the event is zero
CCheck if the event is true
DNo need to check
Can you declare an event with a custom delegate type?
ANo
BOnly with EventHandler
CYes
DOnly with Func delegates
Which of these is a valid event declaration?
Aevent public DataReceived EventHandler;
Bpublic delegate event DataReceived;
Cpublic event DataReceived delegate;
Dpublic 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.