Recently I have been using anonymous method a lot, so I wonder if I do like:
Say in my MainWindow class, I have:
ChildWindow myChildWindow = new ChildWindow();
myChildWindow.Closing+= (o, e) => {
//DoSomething
rootGrid.Children.Remove(o as ChildWindow);
}
this.rootGrid.Children.Add(myChildWindow );
myChildWindow.Show();
In this case I won’t be able to remove the event handler from .Closing since is defined with anonymous method.
Will this ChildWindow still be collected by GC?
Yes.
All you’re doing is creating a delegate with closure object and referencing that from the child window.
You aren’t creating any additional references to the child window.