I am using windows forms.
My C# application contains 100 user controls
. I show/hide one of those 100 user controls
at a time when I need to and hide the rest.
Each one of those user controls
has 30 buttons
and I subscribe to button
event as following in the constructor:
public UserControl1()
{
InitializeComponent();
button1.Click += new EventHandler(MyButtonClick);
button2.Click += new EventHandler(MyButtonClick);
.
.
button30.Click += new EventHandler(MyButtonClick);
}
void MyButtonClick(object sender, EventArgs e)
{
// do something
}
So when I run the Application all the 100 User controls
subscribe to the 30 buttons
event and some of the user controls
subscribe to the event but they are never used during the use of the application.
I read something about unsubscribing events here and Here but some answers say you should unsubscribe because it cause memory leak and some say you don't have to, therefore the answer is still not clear.
My question is do I have to unsubscribe from button
events after using it for example: when I show/hide a user control
. If yes, how can I subscribe from button
event when a user control
is shown and unsubscribe when it is not shown.