By default when you create a Win-Form a application, This is the code generated by visual studio to dispose the Form.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Is this enough?Or I should unregister all events so the controls will be ready to collected by the garbage collector?
if (disposing && (components != null))
{
myButton.OnClick-= MyFunction; //may be here!!
// ... all events used
components.Dispose();
}
No, the garbage collector takes care of it. The event can never be raised anymore since the form instance was disposed which in turn disposed the button. There is a circular reference between the form and the button due to the event handler but the garbage collector has no trouble with them.