Observer, Delegates, and .NET Events

What is this series of articles?

I'm a fairly prolific poster on the Xtreme Visual Basic Talk forums in an attempt to pay back the excellent help I got when I was just a .NET newbie. I've written some fairly extensive tutorials on events. After getting the second tutorial moved to the "Tutors' Corner" section of the forums, I got a private message from a friend expressing confusion over .NET events. Just what are they? How are they implemented? Why do I need a delegate? Just what is a delegate anyway?

These are good questions, and my tutorials didn't really cover them from the angle that would have answered them. I had assumed an audience that was familiar with .NET events and willing to learn how to use them better; I forgot about the audience that wasn't already familiar with .NET events. This came up when I just happened to be finishing some study of design patterns, and the Observer pattern seemed like an excellent way to start explaining events.

So this is the result of the idea. If you are a novice .NET developer or you can't answer one or more of the questions above, these articles are for you. If you're an advanced .NET developer but you don't know how events are related to delegates, this article's for you. I'm going to start by talking about the Observer pattern; this is what programmers used before events existed and they had to make their 1s by banging rocks together to make a spark. Then I'm going to introduce you to delegates and demonstrate how you can use delegates to implement the Observer pattern. Then I'm going to introduce you to multicast delegates, and show you how they make it easier to implement the Observer pattern. Finally, I'll wrap things up and reveal how events are really multicast delegates in a fancy box. When we're done, your mind will be blown and you'll be a better programmer for it. Let us begin.

Table of Contents

First, get yourself the example project. For now, it's only available in VS 2008 format but I might create a VS 2005 version.

Further Reading