.NET Events - What are object sender EventArgs e?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, event handlers commonly use the signature object sender, EventArgs e. This pattern gives subscribers both the event source and any attached payload while keeping publisher and listener loosely coupled. Understanding why this signature exists helps you design clear event contracts in desktop apps, services, and reusable libraries.
Core Sections
Why the Signature Uses sender and EventArgs
sender is the object that raised the event. It lets one handler process events from multiple publishers and branch on source when needed.
EventArgs is a payload object. When no payload is needed, EventArgs.Empty communicates that explicitly without allocating new objects.
Basic event declaration and raise pattern:
Subscribers receive both parameters and can decide how much they care about each.
Creating Strongly Typed Event Data
Real systems usually need payload fields, so define custom event argument classes derived from EventArgs.
Now subscribers get compile time access to FileName and RecordCount without casting from untyped state.
Subscribing, Unsubscribing, and Lifetime Safety
Handlers are attached with += and removed with -=. In long lived applications, forgetting to unsubscribe can keep objects alive unexpectedly.
If publisher lifetime exceeds subscriber lifetime, always remove handlers during disposal or shutdown hooks.
Interpreting sender Safely in Handlers
Do not assume sender is always a specific type unless the event contract guarantees it. Use pattern matching for safety.
This keeps handlers robust when reused across contexts.
Threading and Async Boundaries
Events run on the thread that raises them. In UI frameworks, background threads cannot update controls directly, so handlers often need dispatch back to the UI thread.
For service code, avoid blocking handlers that run on critical worker threads. If heavy processing is required, queue work and return quickly.
For async workflows, event handlers can call async methods, but keep exception handling explicit because event invocation chains can hide failures.
Design Guidelines for Maintainable Events
Events are best for notifications, not command execution. Keep payloads focused on what changed, avoid mutable shared objects, and document ordering expectations.
Good event design checklist:
- Name event by past tense action, for example
CompletedorSaved. - Keep payload small and immutable.
- Define clear publisher ownership and unsubscription policy.
- Avoid cross layer control flow hidden behind events.
These rules make event driven systems easier to test and reason about.
Common Pitfalls
- Casting
senderblindly without type checks. - Using events as hidden command channels instead of notifications.
- Forgetting to unsubscribe handlers from long lived publishers.
- Passing mutable global state through event payloads.
- Running expensive logic directly inside publisher thread handlers.
Summary
senderidentifies the publisher instance that raised the event.EventArgscarries payload, orEventArgs.Emptywhen no payload is needed.- Use
EventHandler<TEventArgs>for strongly typed event data. - Manage handler lifetimes carefully to avoid retention bugs.
- Keep event contracts simple, explicit, and notification focused.
Related reading
- .NET Format a string with fixed spaces
- .NET Geometry Library
- .NET Global exception handler in console application
- .NET graph library around?
- .NET How to check if path is a file and not a directory?
- .NET How to have background thread signal main thread data is available?
- .Net HttpWebRequest.GetResponse raises exception when http status code 400 bad request is returned
- .net implementation of bcrypt

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.