Registering Methods with Delegates the Easy Way

Every C# developer is familiar with the syntax for registering a method to a delegate or event:

_myButton.Click += new EventHandler(myButton_Click);

I'm finding that a lot of people don't seem to know about the alternate "shorthand" syntax for doing the same thing that was introduced in .NET 2.0:

_myButton.Click += myButton_Click;

I mention this because I feel that the shorthand syntax is preferable in a number of ways, starting with the fact that it's shorter, therefore eliminating some of the normal boilerplate cruft and leaving you with more readable code. It's also easier to understand for developers new to C#. Consider the following:

_myButton.Click += new EventHandler(myButton_Click);
_myButton.Click -= new EventHandler(myButton_Click);

Every C# developer eventually learns that even though you created two separate delegate objects in this example, the myButton_Click method still gets unregistered. The reason is that when the delegate is looking for a delegate to unregister, it doesn't compare delegates by reference, but instead by the underlying method(s) they will call. I'll be the first to admit that I found this behaviour a little counterintuitive when I started coding C#.

Now, compare the previous example to the same code, but using shorthand notation:

_myButton.Click += myButton_Click;
_myButton.Click -= myButton_Click;

Regardless of how much C# you now, it's pretty obvious here that myButton_Click is going to be unregistered by the time this snippet finishes executing.

Addendum — While looking around for delegate information, I came across a really great article on weak event listeners in C#. This is an important concept because despite all the memory safety C# provides to you, it's still easy to get memory leaks from objects dangling by a single delegate reference that wasn't property unregistered.

Posted on June 25, 2009 from Calgary

Newest Articles

About

My name is Brandur. I'm a polyglot software engineer and part-time designer working at Heroku in San Francisco, California. I'm a Canadian expat. My name is Icelandic. Drop me a line at brandur@mutelight.org.

Aside from technology, I'm interested in energy and how it relates to our society, travel, longboarding, muay thai, symphonic metal, and the guitar.

If you liked this article, consider finding me on Twitter.