Say I have a Dialog class that extends a form. There is a text field in the dialog box and the OK button, and when the user clicks OK, the value of the text field is returned through the event:
public class Dialog: Form { public delegate void onDialogValueReturned(object sender, DialogEventArgs e); public event onDialogValueReturned DialogValueReturned; . . . OKButton.Click += (sender, evt) => { DialogEventArgs e = new DialogEventArgs(); e.Value =myTextBox.Text; DialogValueReturned(this, e); this.Close(); };
In my call form, I create a dialog in the local method:
private void Foo() { Dialog D = new Dialog("blah blah"); D.DialogValueReturned += (dialog, evt) => { //do something with evt.Value }; D.ShowDialog(); }
During the day, this dialogue can be triggered dozens or even hundreds of times.
Does the garbage collector collect everything related to the dialogue instance when the area exits the private method, including all the plumbing for the anonymous listener?
thanks
source share