#576 – How (Traditional CLR) Events Work

An event in .NET is a mechanism that allows an object to inform client code about something that happened inside the object that the other code might care about.

For example, you might have a Dog object that raises a Barked event whenever someone calls the dog’s Bark method.  Your code can be notified whenever the dog barks.

In .NET, we say that the Dog object raises the Barked event.  The logging code handles the Barked event in a method known as an event handler.

Events in C# implement something known as the Publish/Subscribe pattern.  The Dog class publishes information about when the dog barks and the logging code subscribes to that information. 

This is also known as the Observer pattern.  The logging code observes the Dog object and the Dog object notifies any observers whenever the dog barks.

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: