Events let parts of your program talk to each other when something happens. Declaring an event sets up a way to send these messages.
Event declaration syntax in C Sharp (C#)
access_modifier event EventHandler EventName;
// Or with custom delegate:
access_modifier event DelegateType EventName;access_modifier is usually public, private, or protected.
EventHandler is a common delegate type for events that don't send extra data.
Clicked using the built-in EventHandler delegate.public event EventHandler Clicked;DataChanged that sends custom data with MyEventArgs.private event EventHandler<MyEventArgs> DataChanged;ThresholdReachedHandler and an event ThresholdReached using it.public delegate void ThresholdReachedHandler(int threshold); public event ThresholdReachedHandler ThresholdReached;
This program declares an event named SomethingHappened. It subscribes a simple message to it and then triggers the event, causing the message to print.
using System; class Program { // Declare an event using EventHandler delegate public event EventHandler SomethingHappened; public void TriggerEvent() { // Check if there are subscribers SomethingHappened?.Invoke(this, EventArgs.Empty); } static void Main() { Program p = new Program(); // Subscribe to the event p.SomethingHappened += (sender, e) => { Console.WriteLine("Event was triggered!"); }; // Trigger the event p.TriggerEvent(); } }
Always check if the event has subscribers before triggering it to avoid errors.
Events use delegates behind the scenes to hold references to subscriber methods.
Use ?.Invoke syntax to safely call events in modern C#.
Events let objects send messages to others when something happens.
Declare events with the event keyword and a delegate type.
Use ?.Invoke to trigger events safely.
Practice
Solution
Step 1: Understand event declaration basics
In C#, events are declared using theeventkeyword followed by a delegate type.Step 2: Identify the correct keyword
The keywordeventspecifically marks a member as an event, enabling subscription and notification.Final Answer:
event -> Option CQuick Check:
Event declaration uses 'event' keyword [OK]
- Using 'delegate' instead of 'event' keyword
- Confusing event with method declaration
- Trying to declare event without a delegate type
OnClick using a delegate Action?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 AQuick Check:
Correct event syntax = public event Action OnClick; [OK]
- Adding parentheses after event name
- Placing 'event' after delegate type
- Combining 'delegate' and 'event' keywords incorrectly
using System;
class Program {
public static event Action OnStart;
static void Main() {
OnStart += () => Console.WriteLine("Started");
OnStart?.Invoke();
}
}
Solution
Step 1: Understand event subscription and invocation
The eventOnStartis 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 DQuick Check:
Subscribed event invoked prints 'Started' [OK]
- Assuming event is null and no output
- Confusing event invocation syntax
- Expecting compilation or runtime errors
public class Button {
public event Action OnClick();
}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 afterOnClickfixes the syntax:public event Action OnClick;Final Answer:
Remove parentheses after OnClick -> Option BQuick Check:
Events declared without parentheses [OK]
- Adding parentheses after event name
- Using 'delegate' keyword instead of 'event'
- Trying to declare event as a method
DataReceived that uses a custom delegate DataHandler accepting a string parameter. Which is the correct declaration?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 AQuick Check:
Delegate declared first, event uses delegate type [OK]
- Trying to declare event with parameters directly
- Mixing delegate and event keywords
- Adding parentheses after event name
Ready to prove it?
Pass the test and this pattern moves unknown
