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

Event-driven design pattern in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Event-driven design pattern
Start Program
Define Event & Handler
Subscribe Handler to Event
Trigger Event
Event Handler Executes
Continue Program or Exit
The program defines an event and a handler, subscribes the handler to the event, then triggers the event which causes the handler to run.
Execution Sample
C Sharp (C#)
using System;

class Program {
  public static event Action OnJump;
  static void Main() {
    OnJump += () => Console.WriteLine("Jumped!");
    OnJump?.Invoke();
  }
}
This code defines an event OnJump, subscribes a handler that prints 'Jumped!', then triggers the event.
Execution Table
StepActionEvent StateHandler CalledOutput
1Program startsOnJump = nullNo
2Subscribe handler to OnJumpOnJump = handler addedNo
3Invoke OnJump eventOnJump has 1 handlerYesJumped!
4Program endsOnJump unchangedNo
💡 Program ends after event handler runs once
Variable Tracker
VariableStartAfter Step 2After Step 3Final
OnJumpnullhandler subscribedhandler subscribedhandler subscribed
Key Moments - 3 Insights
Why do we use OnJump += handler instead of OnJump = handler?
Using += adds the handler to the event's invocation list without removing others. This is shown in step 2 of the execution_table where the handler is added, allowing multiple handlers if needed.
What does the ?.Invoke() syntax do?
The ?.Invoke() safely calls the event only if it has handlers subscribed. In step 3, it ensures the handler runs only if OnJump is not null, preventing errors.
When does the event handler run?
The handler runs exactly when the event is invoked, as shown in step 3 where the output 'Jumped!' appears after OnJump?.Invoke() is called.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of OnJump after step 2?
AOnJump has no handlers
BOnJump is null
COnJump has one handler subscribed
DOnJump is removed
💡 Hint
Check the 'Event State' column in row for step 2 in execution_table
At which step does the event handler print 'Jumped!'?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Output' column in execution_table for when 'Jumped!' appears
If we remove the handler subscription line, what happens when OnJump?.Invoke() is called?
AProgram crashes with error
BNothing happens, no output
CHandler runs anyway
DEvent unsubscribes itself
💡 Hint
Refer to the meaning of ?.Invoke() in key_moments and execution_table step 3
Concept Snapshot
Event-driven design uses events to signal actions.
Define an event and handlers.
Subscribe handlers with +=.
Trigger event with ?.Invoke().
Handlers run when event triggers.
Full Transcript
This example shows how event-driven design works in C#. First, the program defines an event called OnJump. Then, it subscribes a handler that prints 'Jumped!' when the event happens. When the program calls OnJump?.Invoke(), it triggers the event, causing the handler to run and print the message. The ?.Invoke() syntax ensures the event is only called if there are handlers subscribed, preventing errors. This pattern lets programs respond to actions flexibly by running code only when events occur.