so lately i've been working with disposable object and i was wondering would it be benific to dispose an object inside a function ? like between those 2 functions is the use of .Dispose() really matters inside a function since all objects within a function will vanish as soon as it finishes
void FOO()
{
var x= new DisposableObject();
//stuff
}
void FOO()
{
using(var x= new DisposableObject())
{
//stuff
}
}
You should always Dispose()
an object that needs it. Even if the object is garbage collected, there could be unmanaged resources that don't get released. Calling Dispose()
(or a using
as in your second example) ensures that the object can properly release unmanaged resources that it needs to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With