Dispose Question?

  • Thread starter Thread starter David W
  • Start date Start date
D

David W

Hi

If I have a class that has lets say a big byte array or something, can I
just have a dispose method with nothing in it yet still use the using
statement so that the byte array will get freed immediatly. The other
alternative I guess is I could call GC.Collect, but I think the using
statement is much nicer.

public class BigClass : IDisposable
{
byte [] buffer = new byte[10000000];

void Dispose()
{
//No code
}
}

using( BigClass bigclass)
{

}

Thanks.
 
David,

If you are using the "using" statement, then the idea is that you are
not going to use the instance of the that is the target of the statement
after the block has been exited. Because the array is a managed resource,
you don't have to even have IDisposable implemented. All you have to do is
make sure that all references to the array are set to null. If your class
is the only one that uses it, then you have nothing to worry about.

Now, if you want to implement a GC, then thats another issue. I would
say that this situation doesn't warrant a GC, that if there were some memory
constraints on the system at that time, that a GC would take place
automatically.

Hope this helps.
 
Back
Top