Search code examples
c#asp.netcachingdatatabledispose

Do I have to call dispose after each use though datatable is stored in cache?


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.