In-line and looking hot
Now will all that hype, this is bound to be let down.
You will, all of you, be familiar with anonymous methods in C#2.0 and their most immediately useful application as anonymous delegates:
btnFoo.Click += delegate
{
doSomethingGood();
};
They are great and I've been using them a lot where it simplifies the code and makes it easier to read.
Well today I accidentally did something even cooler - mostly by accident.
Imagine a generic function which creates buttons. One of the parameters is an EventHandler that gets hooked up to the button's Click event:
private void MakeButton(string buttonText, EventHandler e)
{
Button b = new Button();
b.Text = buttonText;
{
b.Click += e;
}
}
MakeButton("Paris", new EventHandler(delegate{this.txtStatus.Text
= "will be fun";}));
MakeButton("The button", new EventHandler(delegate { this.txtStatus.Text
= "has been clicked"; }));
That's really cool! No little event handling methods lying around. Everything contained with in one line. Easy to read. Easy to write. Okay, I'm done now.
As a treat for anyone who's still reading: Laura has some news... (no, not babies) ... she'll post about it soon!
Finally, some decent technical content. Keep this up and I may even become a regular reader ;-)
And congratulations Laura on your news - we are very happy to hear it.