Anonymous Methods ...

I read Anonymous Methods support in Whidbey (CLR) last week and decided to play with it. This looks a lot like how Javascript (in IE) handles event -

<body onload="msgbox('hello')" ..>

In general Anonymous Methods allows code block in place of delegate. For example,

public class TestApp: Windows.Application
{
protected override void
OnStartingUp(Windows.StartingUpCancelEventArgs e)
{
Window win = new Window();
win.Text = "My Avalon Window";
win.SizeChanged +=delegate { win.Text = "My Avalon Window changed"; };
...
}

My first impressions are "how can I debug this in my IDE?" and "isn't this going to make my code messy?".

I did a test with my VS Whidbey PDC and to my suprise, it actually work with the debugging. The following line actually being called twice when my loading my Window.

 win.SizeChanged += delegate { win.Text = "My Avalon Window changed"; };

The first time to setup the SizeChanged event-handling and second (and subsequent) time when my Window's size changed.

This solves my first concern, what about the second one? I thought about it for a while, it's up to you to decide to use it or not. I think Anonymous Method is suitable for a small anonymous method (like setting 1 or 2 properties). Using Anonymous Methods will help me reduce the number of lines in code due to less Event Handlers. For event handlers that do a bunch of things, you can always use stick to using an event handler.

This posting is provided "AS IS" with no warranties, and confers no rights.