Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does the Dispose method inside a function matters?

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
    }
}
like image 716
ihisham Avatar asked Dec 19 '22 22:12

ihisham


1 Answers

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.

like image 134
itsme86 Avatar answered Jan 05 '23 13:01

itsme86