Read some code from a legecy system.
Say I have a instance of class A, then added it to a collection, then dispose it.
I just can not understand what's the point to use implicit dispose here since the code still use the myControlCollcetion,which means the cltCheckBoxA wont be really freed anyway.
Why?
using (UserControlA cltCheckBoxA = new UserControlA())
{
//some operation
base.myControlCollcetion.Add(cltCheckBoxA);
}
//Other operation against myControlCollcetion
Disposing an object is entirely different from freeing it's managed memory (which is what the GC does). Dispose is explicitly cleaning up all unmanaged resources that the garbage collector can't free.
This means that when the object is used after Dispose
is called on it there are a lot of possibilities. (This is not an exhaustive list.)
DataTable
is an example of this; it's Dispose
does nothing. In this case, the object will work just fine after it is disposed.Dispose
is called, in which case as long as only those limited aspects are used later on it will work just fine.