When you create a form or user control, the WinForms constructor generates a delete method that looks like this:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
The problem with this code is that it can lead to incorrect behavior if it is ever edited to remove additional objects. I saw .designer.cs files with dispose methods that look like this:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
if (_myDisposable != null)
_myDisposable.Dispose();
if (_myOtherDisposable != null)
_myOtherDisposable.Dispose();
}
base.Dispose(disposing);
}
... which is wrong, since removing _myDisposable and _myOtherDisposable should not depend on whether the components are null.
, , , , , , : , ?
protected override void Dispose(bool disposing)
{
if (disposing)
{
if(components != null)
components.Dispose();
}
base.Dispose(disposing);
}
, .