2

reference Removing FrameworkElements on Animation Completion

In above question, how come we access 'myrect' in lambda expression but can't access in traditional method.

lambda expression :

SB.Completed += (s,e) => myCanvas.Children.Remove(myRect);

traditional approach :

public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            lastFire = DateTime.Now;
        }

        DateTime lastFire;

        private void myCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            DateTime nowTime = DateTime.Now;
            TimeSpan T = nowTime.Subtract(lastFire);

            if (T.TotalMilliseconds > 200)
            {
                lastFire = nowTime;
                Random Rand = new Random();

                Rectangle myRect = (Rectangle)FindResource("MyRect");
                myRect.Fill = new SolidColorBrush(Color.FromRgb((byte)Rand.Next(256), (byte)Rand.Next(256), (byte)Rand.Next(256)));
                Point myLoc = e.GetPosition(myCanvas);
                Canvas.SetLeft(myRect, myLoc.X - 10);
                Canvas.SetTop(myRect, myLoc.Y - 10);
                myCanvas.Children.Add(myRect);

                Storyboard SB = (Storyboard)FindResource("GrowSquare");
                SB.Completed += new EventHandler(SB_Completed);
                SB.Begin(myRect);
            }

        }

        void SB_Completed(object sender, EventArgs e)
        {
            myCanvas.Children.RemoveAt(0);
        }
    }
2
  • 1
    "but can't access in traditional method." I could not find a single word saying anythign close to that. Please specify where you read it. Commented Jul 30, 2019 at 11:44
  • 2
    If you mean you can't access it in SB_Completed, that's because it's out of scope there. You can pass it to the event handler by creating your own custom EventArgs and create an EvenHandler<CustomEventArgs>. Commented Jul 30, 2019 at 11:45

1 Answer 1

2

As soon as you create a lambda expression the compiler will convert it to a class with the 'method' you created and fields for all the captured variables in the lambda expression. See this talk from David Wengier if you are interested.

So lets take the following code:

public void Foo()
{
   string bar = "Bar";
   Action action = x => Console.WriteLine(bar);
   action();
}

It will convert it to something like the following:

private sealed class Helper {

  public string bar;

  public void action()
  {
     Console.WriteLine(bar);
  }
}

And in your "traditional" method your variable myrect would be out of scope. What are scopes?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @twenty for explaination and video link. Nicely explained in video and loved to know about memory advantage of lambdas.
You are welcome, feel free to mark it as an answer if it helped :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.