Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Event declaration syntax in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Event declaration syntax
Declare delegate type
↓
Declare event using delegate
↓
Subscribe method to event
↓
Raise event to call subscribed methods
↓
End
This flow shows how to declare a delegate type, then declare an event using that delegate, subscribe methods to the event, and finally raise the event to call those methods.
Execution Sample
C Sharp (C#)
public delegate void Notify();
public class Process {
  public event Notify ProcessCompleted;
  public void Start() {
    ProcessCompleted?.Invoke();
  }
}
Declares a delegate, an event using that delegate, and raises the event inside a method.
Execution Table
StepActionEvaluationResult
1Declare delegate NotifyDefines a delegate type with no parametersDelegate type Notify created
2Declare event ProcessCompleted of type NotifyEvent is declared but no subscribers yetEvent ProcessCompleted created, empty subscriber list
3Subscribe method Handler1 to ProcessCompletedAdds Handler1 to subscriber listProcessCompleted subscribers: [Handler1]
4Call Start methodInside Start, ProcessCompleted?.Invoke() calledHandler1 method invoked
5Subscribe method Handler2 to ProcessCompletedAdds Handler2 to subscriber listProcessCompleted subscribers: [Handler1, Handler2]
6Call Start method againProcessCompleted?.Invoke() calls all subscribersHandler1 and Handler2 methods invoked
7Unsubscribe Handler1 from ProcessCompletedRemoves Handler1 from subscriber listProcessCompleted subscribers: [Handler2]
8Call Start method againOnly Handler2 invokedHandler2 method invoked
9EndNo more actionsExecution stops
💡 No more method calls; event raising stops
Variable Tracker
VariableStartAfter 3After 5After 7Final
ProcessCompleted subscribers[][Handler1][Handler1, Handler2][Handler2][Handler2]
Key Moments - 3 Insights
Why do we use a delegate before declaring an event?
The delegate defines the method signature that event handlers must follow. The event uses this delegate type to ensure only compatible methods can subscribe, as shown in step 1 and 2 of the execution_table.
What does the ?.Invoke() syntax mean when raising the event?
It safely calls the event only if there are subscribers. If no one subscribed, it prevents a null reference error. This is shown in step 4 where ProcessCompleted?.Invoke() is called.
Can multiple methods subscribe to the same event?
Yes, multiple methods can subscribe and all will be called when the event is raised. This is shown in steps 5 and 6 where Handler1 and Handler2 are both invoked.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the state of ProcessCompleted subscribers?
A[]
B[Handler1]
C[Handler2]
D[Handler1, Handler2]
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step does the event have two subscribers?
AStep 5
BStep 7
CStep 3
DStep 8
💡 Hint
Look at the 'ProcessCompleted subscribers' in variable_tracker after step 5.
If we unsubscribe Handler2 at step 7 instead of Handler1, what would be the subscribers after step 7?
A[]
B[Handler2]
C[Handler1]
D[Handler1, Handler2]
💡 Hint
Refer to variable_tracker and imagine removing Handler2 instead of Handler1 at step 7.
Concept Snapshot
Event declaration in C#:
- Declare a delegate type for method signature.
- Declare an event using that delegate.
- Subscribe methods to the event.
- Raise event with ?.Invoke() to call subscribers safely.
- Multiple subscribers allowed; unsubscribing removes them.
Full Transcript
This visual execution shows how to declare and use events in C#. First, a delegate type is declared to define the method signature for event handlers. Then, an event is declared using this delegate type. Methods can subscribe to this event, adding themselves to the subscriber list. When the event is raised using ?.Invoke(), all subscribed methods are called. The execution table traces subscribing, raising, and unsubscribing steps, showing how the subscriber list changes. Key moments clarify why delegates are needed, the safe invocation syntax, and multiple subscriptions. The visual quiz tests understanding of subscriber states at different steps. The snapshot summarizes the event declaration syntax and usage in simple steps.

Practice

(1/5)
1. What keyword is used in C# to declare an event?
easy
A. trigger
B. delegate
C. event
D. notify

Solution

  1. Step 1: Understand event declaration basics

    In C#, events are declared using the event keyword followed by a delegate type.
  2. Step 2: Identify the correct keyword

    The keyword event specifically marks a member as an event, enabling subscription and notification.
  3. Final Answer:

    event -> Option C
  4. 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

  1. Step 1: Recall correct event declaration syntax

    The correct syntax is: public event DelegateType EventName; without extra keywords or parentheses.
  2. 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.
  3. Final Answer:

    public event Action OnClick; -> Option A
  4. 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

  1. 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.
  2. Step 2: Predict program output

    Since the event has one subscriber, invoking it prints "Started" to the console.
  3. Final Answer:

    Started -> Option D
  4. 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

  1. Step 1: Analyze event declaration syntax

    Events are declared without parentheses after the event name because they are not methods but event fields.
  2. Step 2: Correct the syntax

    Removing parentheses after OnClick fixes the syntax: public event Action OnClick;
  3. Final Answer:

    Remove parentheses after OnClick -> Option B
  4. 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

  1. Step 1: Declare the delegate type first

    Custom delegates must be declared separately with the correct signature: public delegate void DataHandler(string data);
  2. Step 2: Declare the event using the delegate type

    The event is declared as: public event DataHandler DataReceived; without parentheses.
  3. Final Answer:

    public delegate void DataHandler(string data); public event DataHandler DataReceived; -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard