Partager via


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.

Comments

  • Anonymous
    February 06, 2004
    I remain unconvinced about the real usefullness of these appart from lazyness.

  • Anonymous
    February 06, 2004
    I think this is bad...

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

    The call to win.SizeChanged += blah means SUBSCRIBE to this event. Now what been done here is when the debugger steps to the anonymous code block, its confusing, because when i see += operators on events, i think SUBSCRIBE not do this block of code. Its bad bad bad.
  • Anonymous
    February 06, 2004
    The comment has been removed
  • Anonymous
    February 07, 2004
    The comment has been removed
  • Anonymous
    February 07, 2004
    The comment has been removed
  • Anonymous
    February 07, 2004
    How easy do they want it, i mean in the IDE if you hit TAB it will complete the EventHandler method block for you.
  • Anonymous
    February 07, 2004
    I'm not suggesting to use Anonymous Method to replace all your delegate. This feature is good for certain scenarios like those event handlers that have 1/2 lines in it.

    It's all additional options for developers.
  • Anonymous
    February 07, 2004
    In a direct quote from a previous blog...

    http://blogs.msdn.com/ericgu/archive/2004/02/05/68186.aspx

    "One of our design goals is not to have more than one way of doing something (in contrast to Perl's tmtowtdi). If there are two different ways of doing something, developers need to learn both ways and understand that they are equivalent. That makes their job more complex without any real benefit."


    So, I guess this goes against those "design goals" that where ranted about earlier.

  • Anonymous
    February 07, 2004
    How does one UNSUBSCRIBE (unwire) an anonymous method, we dont know the internal ID thats assigned to this so how is this possible? I think thats where it gets MESSY.

  • Anonymous
    February 08, 2004
    The comment has been removed
  • Anonymous
    February 10, 2004
    moo,

    Do you really take -= on all event-handlers? Look at this small example:
    (this textbox isn't the best editor to use, so please disregard small syntax errors ;))

    public class MyForm : Form
    {
    private Button cmdHitMe;
    private Button cmdOuch;

    public MyForm ()
    {

    // Some code to initialize the buttons and
    // place them on MyForm ommitted for clearity..

    cmdHitMe.OnClick += delegate { MessageBox ("Hello!") ; }
    cmdOuch.OnClick += delegate { cmdHitMe.Enabled = !cmdHitMe.Enabled; }
    }
    }

    In the cmdOuch.OnClick handler.. do you really think it would be better to unwire cmdHitMe.OnClick instead?

    Like other people have said before, use it if you want -- and use it where it's useful :) If the OnClick methods do something more than trivial of course place it in a named method...

    I really can't see your problem with this...?
  • Anonymous
    February 10, 2004
    SO, can we unwire anonymous methods or not?

    I take your response as a NO.
  • Anonymous
    February 11, 2004
    Thanks for reading my comment without understanding it ;) Please read it again and I hope you'll realize that you absolutely not will use anonymous methods everywhere you have the opportunity.

    Just because it's a new language feature doesn't mean you have to use it. Most of the events in a standard Windows Forms or ASP.NET application won't have to be unwired manually, so that's a perfect opportunity to use anonymous methods (if you're gonna just do something quick in that handler...).

    But I assume you're still not convinced that anon. methods are not the next big evil thing introduced in a language since goto...