Should I use Dispose in this situation
-
hello, i'm initiating an object inside a method: private void Example () { MyClass ins = new MyClass(...); ins.AMethod(); ins.Dispose(); } i'm wondering if the last line: ins.Dispose(); is necessary? Or C# does it automaticlly?... i mean disposing the object as soon as the method is finish. please advise
-
hello, i'm initiating an object inside a method: private void Example () { MyClass ins = new MyClass(...); ins.AMethod(); ins.Dispose(); } i'm wondering if the last line: ins.Dispose(); is necessary? Or C# does it automaticlly?... i mean disposing the object as soon as the method is finish. please advise
xkx32 wrote:
i'm wondering if the last line: ins.Dispose(); is necessary? Or C# does it automaticlly?...
To the first question - If the class has a
Dispose()
method then, yes, you should use it. To the second question - If the class has a destructor (Finalizer) that callsDispose()
then, yes, it will eventually get to it. BUT, just because it will get to it eventually doesn't mean you can get all lazy about it. If a class supportsDispose()
it is usually for a very good reason and its very existance suggests that you should use it.xkx32 wrote:
i mean disposing the object as soon as the method is finish
If that is where the object is naturally no longer required then that is where you should dispose of it. If you are only going to use an object within one method and it won't be stored anywhere else by the time the method completed then you may like to use the following instead
using (MyClass ins = new MyClass())
{
ins.AMethod();
}What happens here is that as soon as the code exits the scope of the
using
block for what ever reason (it might reach it naturally, or it might be in the process of throwing an exception) the object will beDispose
d of.
Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
hello, i'm initiating an object inside a method: private void Example () { MyClass ins = new MyClass(...); ins.AMethod(); ins.Dispose(); } i'm wondering if the last line: ins.Dispose(); is necessary? Or C# does it automaticlly?... i mean disposing the object as soon as the method is finish. please advise
The
Dispose
method doesn't dispose of the object itself, it tells the object to dispose of the unmanaged resources that it's using. If an object has a Dispose method, you should use it. It helps the object to free resources as soon as possible. If you don't call Dispose, you won't know when the resources will be freed. As .NET uses garbage collection, not reference counting, the object will not be finalized immediately when there is no reference to it, but when the garbage collector does the next collection. Depending on the memory "turnover" of the program, this can be in seconds, minutes, hours... or not until the application ends.--- b { font-weight: normal; }
-
hello, i'm initiating an object inside a method: private void Example () { MyClass ins = new MyClass(...); ins.AMethod(); ins.Dispose(); } i'm wondering if the last line: ins.Dispose(); is necessary? Or C# does it automaticlly?... i mean disposing the object as soon as the method is finish. please advise
Microsoft technicians state that you should never call Dispose and always allow the Garbage Collector to handle object management. That object exposing the IDispose method places it at the top of the list for GC to process.