Events are the core mechanism for implementing observer patterns in C#. It allows an object to notify the occurrence of a specific action without being tightly coupled to other objects. Events are essentially encapsulation of delegates, allowing classes to expose subscription methods without giving call control. For example, the Click event in the Button class is based on the EventHandler delegation. When the button is clicked, the OnClick method will be called to trigger the event. Key points include: 1. Events can only be called by the class that declares them; 2. Subscribers can dynamically add or remove handlers at runtime. When defining custom events, you can create a class that inherits EventArgs and the corresponding delegate, such as the FileWatcher class passing file change information through the FileChanged event. Best practices for using events are: 3. When subscribers no longer need to update, they should unsubscribe to avoid memory leaks; 4. Prioritize the use of EventHandler
Events in C# are a core part of the language's support for the observer pattern. At their heart, they provide a way for one object to notify other objects when something happens—without those objects needing to be tightly coupled. If you've ever used buttons in a UI framework like WPF or WinForms, you've already worked with events.

What Exactly Is an Event?
An event is essentially a wrapper around a delegate. It allows a class to expose a way for other code to subscribe to certain actions, without exposing full control over invocation. You can think of it like a subscription list: interested parties can register themselves and get notified when a specific action occurs.

For example:
public class Button { public event EventHandler Click; protected virtual void OnClick() { Click?.Invoke(this, EventArgs.Empty); } }
Here, Click
is an event based on the EventHandler
delegate. When someone clicks the button, OnClick
gets called, which in turn raises the event. Any method that matches the signature of EventHandler
can subscribe to this event.

Key points:
- Events can only be invoked from within the class that declares them.
- Subscribers can add (
=
) or remove (-=
) their handlers at runtime.
How to Define Custom Events
While .NET provides many built-in delegates like EventHandler
, sometimes you need your own custom data structure to pass along with the event. This is where defining your own delegate and event args becomes useful.
Let's say you're building a file watcher and want to notify subscribers when a file changes:
Define a custom
EventArgs
class:public class FileChangedEventArgs : EventArgs { public string FileName { get; set; } public DateTime ChangeTime { get; set; } }
Define a matching delegate:
public delegate void FileChangedEventHandler(object sender, FileChangedEventArgs e);
Declare the event in your class:
public class FileWatcher { public event FileChangedEventHandler FileChanged; protected virtual void OnFileChanged(string fileName) { FileChanged?.Invoke(this, new FileChangedEventArgs { FileName = fileName, ChangeTime = DateTime.Now }); } }
This gives you flexibility to carry rich contextual information when raising the event.
Best Practices and Common Pitfalls
Working with events is straightforward, but there are some gotchas:
Avoid memory leaks by unsubscribe : If a subscriber doesn't unsubscribe, the publisher will keep a reference to it, potentially preventing garbage collection. Always remember to use
-=
when the subscriber no longer needs updates.Use
EventHandler<t></t>
instead of creating new delegates : Unless you have a very good reason, prefer using the genericEventHandler<t></t>
rather than defining your own delegate. It reduces boilerplate and improves consistency.Make sure to check for null before invoking : In older versions of C#, you'd write
if (Click != null) Click(...);
. In modern C#, you can safely use the null-conditional operator:Click?.Invoke(...)
. This avoids NullReferenceExceptions if there are no subscribers.Virtual methods for raising events help with inheritance : By making
OnClick
virtual, derived classes can override behavior without breaking encapsulation.
Also, don't confuse events with async notifications—they're not inherently asynchronous. If you need async behavior, wrap the invocation in a task or use
async/await
.
That's how events work under the hood and how to use them effectively. They're a powerful tool once you understand the mechanics.
The above is the detailed content of Understanding the C# Eventing Model in Depth. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

TheyieldkeywordinC#simplifiesiteratorcreationbyautomaticallygeneratingastatemachinethatenableslazyevaluation.1.Itallowsreturningitemsoneatatimeusingyieldreturn,pausingexecutionbetweeneachitem,whichisidealforlargeordynamicsequences.2.yieldbreakcanbeus

DependencyInjection(DI)inC#isadesignpatternthatenhancesmodularity,testability,andmaintainabilitybyallowingclassestoreceivedependenciesexternally.1.DIpromotesloosecouplingbydecouplingobjectcreationfromusage.2.Itsimplifiestestingthroughmockobjectinject

The role of IDisposable and using in C# is to efficiently and deterministically manage unmanaged resources. 1. IDisposable provides Dispose() method, so that the class can clearly define how to release unmanaged resources; 2. The using statement ensures that Dispose() is automatically called when the object is out of scope, simplifying resource management and avoiding leakage; 3. When using it, please note that the object must implement IDisposable, can declare multiple objects, and should always use using for types such as StreamReader; 4. Common best practices include not relying on destructors to clean up, correctly handling nested objects, and implementing the Dispose(bool) pattern.

LambdaexpressionsandLINQsimplifydatamanipulationinC#byenablingconcise,readable,andefficientcode.1.Lambdaexpressionsallowinlinefunctiondefinitions,makingiteasiertopasslogicasargumentsforfiltering,transforming,sorting,andaggregatingdatadirectlywithinme

Nullablereferencetypes(NRTs)inC#8 helpcatchNullReferenceExceptionerrorsatcompiletimebymakingreferencetypesnon-nullablebydefault,requiringexplicitdeclarationfornullability.NRTsmustbeenabledeitherinthe.csprojfilewithenableoratthetopofa.csfileusing#null

Four common "anti-pattern" problems in C# development need to be avoided. First, the unreasonable use of async/await leads to deadlocks or performance degradation. We should adhere to the principle of full asynchronousness, configure ConfigureAwait(false) and standardize naming; second, excessive dependence on var affects readability, and explicitly declare and unify team specifications when the type is unclear; third, the incorrect use of Dispose and resource management causes leakage, and the use statement should be used correctly and the IDisposable standard mode should be implemented; fourth, the abuse of static classes or singletons causes testing difficulties, and priority should be given to dependency injection, statelessness, or the life cycle managed by containers. Avoiding these misunderstandings can significantly improve code quality and maintenance.

Span and Memory improve C# performance by reducing memory allocation. 1. Span avoids array copying and provides light references to existing memory, which is suitable for parsing binary protocols, string operations and high-performance buffer management; 2. Memory supports passing memory slices across asynchronous methods, which is suitable for scenarios where more flexible life cycles are required; 3. Both reduce GC pressure, optimize performance by reusing buffers and avoiding temporary copying; 4. Span is limited to use on the stack and cannot be stored in classes or used in asynchronous methods. Be careful to avoid reassignment operations such as calling.ToArray().

SOLID principle is five design principles to improve code maintainability and scalability in object-oriented programming. They are: 1. The single responsibility principle (SRP) requires that the class only assumes one responsibility, such as separating report generation and email sending; 2. The opening and closing principle (OCP) emphasizes that the extension is supported through interfaces or abstract classes without modifying the original code, such as using the IShape interface to realize area calculation of different graphics; 3. The Richter replacement principle (LSP) requires that the subclass can replace the parent class without destroying logic, such as Square should not mistakenly inherit Rectangle, resulting in abnormal behavior; 4. The interface isolation principle (ISP) advocates the definition of fine-grained interfaces, such as split printing and scanning functions to avoid redundant dependencies; 5. The dependency inversion principle (DIP) advocates the
