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

Custom event arguments in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom event arguments
Define EventArgs subclass
Create event using custom args
Subscribe event handler
Raise event with custom args
Handler receives custom args
Use data from custom args
This flow shows how to create and use custom event arguments in C# events, from defining the args class to handling the event.
Execution Sample
C Sharp (C#)
public class MyEventArgs : EventArgs {
  public int Value { get; }
  public MyEventArgs(int value) => Value = value;
}

public event EventHandler<MyEventArgs> MyEvent;

MyEvent?.Invoke(this, new MyEventArgs(42));
Defines a custom event argument class with a value, then raises an event passing that value.
Execution Table
StepActionEvaluationResult
1Define MyEventArgs classClass created with int Value propertyReady to use custom args
2Declare event MyEvent with MyEventArgsEvent handler type setEvent ready for subscription
3Subscribe handler to MyEventHandler method linkedHandler will be called on event
4Raise MyEvent with new MyEventArgs(42)Invoke event if not nullHandler called with Value=42
5Handler receives event argsAccess args.ValueHandler uses Value=42
6End of event raisingNo more handlersExecution continues
💡 Event raised once, handler executed, no more events
Variable Tracker
VariableStartAfter Step 4After Step 5Final
MyEventnull or emptyHas handler subscribedHandler invokedRemains subscribed
MyEventArgs instancenoneCreated with Value=42Passed to handlerUsed in handler
Key Moments - 3 Insights
Why do we create a class inheriting from EventArgs?
Because custom event data needs a container class; inheriting EventArgs is the standard pattern, as shown in Step 1 of the execution_table.
How does the event handler receive the custom data?
The handler gets the custom args object as a parameter when the event is invoked, shown in Step 5 where args.Value is accessed.
What happens if no handler is subscribed when the event is raised?
The event invocation checks for null (Step 4), so if no handler is subscribed, nothing happens and no error occurs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what value is passed in the custom event args?
A0
B42
Cnull
DEventArgs default
💡 Hint
Check the 'Result' column in Step 4 showing 'Handler called with Value=42'
At which step does the event handler get called?
AStep 5
BStep 2
CStep 4
DStep 3
💡 Hint
Step 5 shows 'Handler receives event args' and uses the value
If no handler is subscribed, what happens when the event is raised?
ANullReferenceException occurs
BProgram crashes
CEvent is ignored safely
DHandler is called with null args
💡 Hint
Step 4 mentions 'Invoke event if not null' meaning it checks before calling
Concept Snapshot
Custom event arguments in C#:
- Create a class inheriting EventArgs with your data
- Declare event using EventHandler<T> with your class
- Raise event passing new instance with data
- Handlers receive your custom data via args parameter
- Always check event is not null before invoking
Full Transcript
This visual execution shows how to use custom event arguments in C#. First, you define a class inheriting from EventArgs to hold your custom data. Then you declare an event using EventHandler with your custom args type. You subscribe a handler method to this event. When you raise the event, you create a new instance of your custom args with the data you want to send and invoke the event. The handler receives this custom args object and can access the data inside. The execution table traces these steps clearly, showing variable states and event flow. Key moments clarify why we inherit EventArgs and how handlers get the data. The quiz tests understanding of the event data, handler call timing, and safe invocation. This pattern helps pass extra information with events in a clean, standard way.