Lamda (=>) expressions

MielieSpoor

Expert Member
Joined
Dec 6, 2006
Messages
1,984
Reaction score
4
Location
Dublin
I saw in a post sometime this week somebody moaned a bit about this expression.

Yes, it has got pro's and con's (like everything in life), of which on con I would say is readability.

One cool thing about it would can be highlighted using the following example:
Code:
Timer timer = new Timer();
timer.Ineterval = TimeSpan.FromSeconds(1);
timer.Tick += (sender, args) =>
  {
      if(count > 3)
      {
          // do something random
      }
      // do something random 2
  };

timer.Start();

Yes I know some of you won't like this, but there are some instances where this type of thing can come in handy!
 
as long as its kiss, used in the right places for the right things and not the one liner of hell im all for it.
 
It's like Javascript's equivalent of:

Code:
var buttons = document.getElementByTagName("input");
for (var i = 0; i < buttons.length; i++)
{
    if (buttons[i].className == "button")
    {
        buttons[i].onclick = function()
            {
                alert("You just clicked the \"" + buttons[i].value + "\" button!");
            };
    }
}

That's what I love about Javascript - the ability to treat a function as a variable! :p

EDIT: Mielies, you should share it in the newly-created .NET Knowledge Sharing thread, stickied at the top of this forum.
 
C# 2.0 Version
Code:
Timer timer = new Timer();
timer.Ineterval = TimeSpan.FromSeconds(1);
timer.Tick += delegate(object timerSender, EventArgs tArgs)
  {
      if(count > 3)
      {
          // do something random
      }
      // do something random 2
  };

timer.Start();

One of the pitfalls of this method is that say timer is local, then there is the risk that timer could get GC'd before the event is fired, because once it goes out of scope there is nothing referencing because the Tick event is bound to an anonymous method. Is this still the case with Lamba?

Aside: If there is consensus that threads like this should go into the Knowledge Sharing Sticky, PM me and I'll add a reference to the entire thread under one the applicable section. Saves everyone from double posting all over the place.
 
C# 2.0 Version
Code:
Timer timer = new Timer();
timer.Ineterval = TimeSpan.FromSeconds(1);
timer.Tick += delegate(object timerSender, EventArgs tArgs)
  {
      if(count > 3)
      {
          // do something random
      }
      // do something random 2
  };

timer.Start();

One of the pitfalls of this method is that say timer is local, then there is the risk that timer could get GC'd before the event is fired, because once it goes out of scope there is nothing referencing because the Tick event is bound to an anonymous method. Is this still the case with Lamba?
Its true but like in all cases you should know when what code could out of scope and when what should not be used.

This is maybe not the best example, but it is the easiest to illustrate on what actually goes on.
 
One of the pitfalls of this method is that say timer is local, then there is the risk that timer could get GC'd before the event is fired, because once it goes out of scope there is nothing referencing because the Tick event is bound to an anonymous method. Is this still the case with Lamba?

Not true. Even if the event handler was hooked up to a regular instance/static method the timer could still be GC'd.
 
Not true. Even if the event handler was hooked up to a regular instance/static method the timer could still be GC'd.

Interesting I always thought that was considered a strong reference...
 
Top
Sign up to the MyBroadband newsletter
X