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: 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: public delegate void MyDelegate(string message); 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: MyEvent?.Invoke(this, EventArgs.Empty);
Click to reveal answer
Which keyword is used to declare an event in C#?
Ainterface
Bevent
Cclass
Ddelegate
✗ Incorrect
The event keyword is used to declare events in C#.
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)
✗ Incorrect
EventHandler has the signature void(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
✗ 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?
ANo
BOnly with EventHandler
CYes
DOnly with Func delegates
✗ Incorrect
Events can use any delegate type matching the signature you want.
Which of these is a valid event declaration?
Aevent public DataReceived EventHandler;
Bpublic delegate event DataReceived;
Cpublic event DataReceived delegate;
Dpublic event EventHandler DataReceived;
✗ 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.
Practice
(1/5)
1. What keyword is used in C# to declare an event?
easy
A. trigger
B. delegate
C. event
D. notify
Solution
Step 1: Understand event declaration basics
In C#, events are declared using the event keyword followed by a delegate type.
Step 2: Identify the correct keyword
The keyword event specifically marks a member as an event, enabling subscription and notification.
Final Answer:
event -> Option C
Quick Check:
Event declaration uses 'event' keyword [OK]
Hint: Events always start with the 'event' keyword in C# [OK]
Common Mistakes:
Using 'delegate' instead of 'event' keyword
Confusing event with method declaration
Trying to declare event without a delegate type
2. Which of the following is the correct syntax to declare an event named OnClick using a delegate Action?
easy
A. public event Action OnClick;
B. public delegate event Action OnClick;
C. event public Action OnClick();
D. public Action event OnClick;
Solution
Step 1: Recall correct event declaration syntax
The correct syntax is: public event DelegateType EventName; without extra keywords or parentheses.
Step 2: Analyze each option
public event Action OnClick; matches the correct syntax exactly. Options A, B, and D misuse keyword order or add invalid syntax.
Final Answer:
public event Action OnClick; -> Option A
Quick Check:
Correct event syntax = public event Action OnClick; [OK]
Hint: Remember: 'event' comes before delegate type, no parentheses [OK]
Common Mistakes:
Adding parentheses after event name
Placing 'event' after delegate type
Combining 'delegate' and 'event' keywords incorrectly
3. What will be the output of the following code?
using System; class Program { public static event Action OnStart; static void Main() { OnStart += () => Console.WriteLine("Started"); OnStart?.Invoke(); } }
medium
A. No output
B. Compilation error
C. Runtime error
D. Started
Solution
Step 1: Understand event subscription and invocation
The event OnStart is subscribed with a lambda that prints "Started". The ?.Invoke() safely calls the event if not null.
Step 2: Predict program output
Since the event has one subscriber, invoking it prints "Started" to the console.
Final Answer:
Started -> Option D
Quick Check:
Subscribed event invoked prints 'Started' [OK]
Hint: ?.Invoke calls event only if not null, so output prints [OK]
Common Mistakes:
Assuming event is null and no output
Confusing event invocation syntax
Expecting compilation or runtime errors
4. Identify the error in the following event declaration and fix it:
public class Button { public event Action OnClick(); }
medium
A. Change 'event' to 'delegate'
B. Remove parentheses after OnClick
C. Add 'void' before event keyword
D. Add parentheses after event keyword
Solution
Step 1: Analyze event declaration syntax
Events are declared without parentheses after the event name because they are not methods but event fields.
Step 2: Correct the syntax
Removing parentheses after OnClick fixes the syntax: public event Action OnClick;
Final Answer:
Remove parentheses after OnClick -> Option B
Quick Check:
Events declared without parentheses [OK]
Hint: Events are fields, not methods--no parentheses after name [OK]
Common Mistakes:
Adding parentheses after event name
Using 'delegate' keyword instead of 'event'
Trying to declare event as a method
5. You want to declare an event DataReceived that uses a custom delegate DataHandler accepting a string parameter. Which is the correct declaration?
hard
A. public delegate void DataHandler(string data); public event DataHandler DataReceived;
B. public event void DataHandler(string data); public DataReceived event;
C. public event DataHandler(string data) DataReceived;
D. public DataHandler event DataReceived(string data);
Solution
Step 1: Declare the delegate type first
Custom delegates must be declared separately with the correct signature: public delegate void DataHandler(string data);
Step 2: Declare the event using the delegate type
The event is declared as: public event DataHandler DataReceived; without parentheses.
Final Answer:
public delegate void DataHandler(string data); public event DataHandler DataReceived; -> Option A
Quick Check:
Delegate declared first, event uses delegate type [OK]
Hint: Declare delegate first, then event with delegate type and no parentheses [OK]
Common Mistakes:
Trying to declare event with parameters directly
Mixing delegate and event keywords
Adding parentheses after event name
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.