Do I have to call dispose after each use though datatable is stored in cache?
Question
Simple case:
i put a DataTable in Cache
DataTable table = SomeClass.GetTable();
Cache["test"] = table;
then in later calls i use
DataTable table = (DataTable)Cache["test"];
now the question is: should i call table.dispose() on each call, though its stored in the Cache? Means the object is always the same? Or will Cache create a copy on each time?
thx :)
Solution
All you are doing is storing a pointer in cache... The actual "table" is still on the heap, where all .Net reference types are stored... You are not making a copy of it... The variable in cache just acts to stop the garbage collector from erasing the object on the heap...
and no, you don't want to call dispose until the actual object is no longer required.
OTHER TIPS
i believe that you should only call Dispose once when you are completely done with the datatable; calling Dispose changes the state of the object.
note that 'cached' does not always mean 'copied'!
a DataTable is a managed resource and does not require that it's Dispose() method be called in order to be properly cleaned up by garabage collection.