views:

1025

answers:

4

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 :)

A: 

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'!

Steven A. Lowe
+5  A: 

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.

Charles Bretana
+1 for clear answer
Steven A. Lowe
A: 

thank you very much. this is a real great community!

A: 

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.