Which one is the right one?
This one:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
this.ctlRBContent1_GetBuildName -= _ctlBottom.GetBuildName;
components.Dispose();
}
base.Dispose(disposing);
}
OR
This one:
~ ctlRemoteBuildContent1()
{
this.ctlRBContent1_GetBuildName -= _ctlBottom.GetBuildName;
}
I tried to toggle this 2 functions. When I did UserControl disposing, it didn’t jump into the toggle line :/
If you clean up in a destructor, there’s no guarantee as to when the destructor will be run. Objects with a destructor also require more work for the garbage collector to handle. It’s preferable therefore to implement IDisposable, which is your first example.
Note that your code snippet does not fully implement the recommended IDisposable pattern. Please see this article for a good and complete explanation:
http://www.csharphelp.com/2010/02/c-garbage-collecting-destructors-versus-dispose/
In your code snippet, if for some reason
componentsis null, you would not remove the event handler. The null check forcomponentsshould only be done to protect thecomponents.Dispose()call.