No longer updated: new and updated blog (including old content) at

Archive for, please update your bookmarks

Quick Tips: C# inline event handlers and Reordering method / constructor parameters

Posted by Roy Triesscheijn on Thursday 15 October, 2009

I always hate having to write an extra method to deal with event handlers and today it irritated me more than ever. The extra event handlers all consisted of just one line of code, it’s also harder to read what is going on if you have to scroll down all the time to check what that event handler is doing.

So today I finally typed in “C# inline event handlers” in Google. And the first website I clicked already gave me what I was looking for. Apparently this was added in C# 2.0 and it basically boils down to this:

this.button1.Click +=
  delegate(object sender, EventArgs e)
  {    MessageBox.Show("Test");   };

Quick easy and very handy!

Another thing that I found out recently is that you can easily reorder method and constructor parameters using Visual Studio 2008’s built in refactoring tools. All you have to do is right click the method/constructor and select Reorder parameters. Very easy and very handy. Just the kind of trick you have to know that is there to be able to use it.

Reorder context

You’ll get a nice form which allows you to reorder your parameters, Visual Studio will automatically update all callers.

Of course these are all tips that are simple and are well documented on the internet, if you look for them,  but maybe I’ll make a few of you guys happy with these.


4 Responses to “Quick Tips: C# inline event handlers and Reordering method / constructor parameters”

  1. Travus said

    Very cool!

    Didn’t know that you could do inline event handlers in C#, very handy and clean!

    • Slobodan said

      Wonder if this leads to memory leak. How do we unsubscribe?

      • Nicki said

        Ha ha memory leaks?
        Welcome to the wonderfull world of .Net, where memory leaks is something you want to avoid after a night in town, instead of in your software…

        • Thomas B said

          yeah except that your object will not be released if a handler references it, unless the handler is also disposed. your app will consume more memory during its runtime than necessary, and depending on how you write your code, this could be a *lot* more memory.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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: