Event declaration syntax in C Sharp (C#) - Time & Space Complexity
When we declare events in C#, it's important to know how the program handles these declarations as the code grows.
We want to understand how the time to declare and use events changes as the number of events or handlers increases.
Analyze the time complexity of the following event declaration and usage.
public class Button
{
public event EventHandler? Clicked;
public void OnClick()
{
Clicked?.Invoke(this, EventArgs.Empty);
}
}
This code declares an event named Clicked and invokes it when OnClick is called.
Look at what happens when the event is invoked.
- Primary operation: Invoking all subscribed event handlers.
- How many times: Once per subscribed handler each time
OnClickis called.
As the number of subscribed handlers grows, the time to invoke the event grows too.
| Number of Handlers (n) | Approx. Invocations |
|---|---|
| 10 | 10 calls to handlers |
| 100 | 100 calls to handlers |
| 1000 | 1000 calls to handlers |
Pattern observation: The time to invoke grows directly with the number of handlers.
Time Complexity: O(n)
This means the time to invoke the event grows linearly with the number of subscribed handlers.
[X] Wrong: "Invoking an event always takes the same time no matter how many handlers are subscribed."
[OK] Correct: Each subscribed handler is called one by one, so more handlers mean more calls and more time.
Understanding how event invocation time grows helps you write responsive programs and shows you know how C# events work under the hood.
What if we changed the event to use a custom delegate that calls multiple methods internally? How would the time complexity change?
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
