I have a Windows.Forms application with a ListBox populated with Account objects. When the user selects an account from the list, I attach the EventHandler, who is responsible for updating the selected account transactions, in case new users appear during the search.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var selected = listBox1.SelectedItem as Account;
if (selected != null)
{
UpdateTransactions(selected);
selected.OnNewTransaction += (s, a) => UpdateTransactions(selected);
}
}
Then my question is: Is this event handler automatically deleted as soon as the user selects another account from the list and the selected account goes out of scope? Or does it continue to linger, and then, if the user selects the same account again, another handler is assigned, thereby creating a memoryleak?
Frank