What is difference between Dispose and Using?
What is difference between Dispose and Using? How to decide which one to use? Which is better option of garbage collection?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Akhil MittalPosted Jun 5, 2014, 1:23 AM
VulpesPosted Jun 4, 2014, 4:21 PM
It's therefore a convenient alterative to calling the Dispose() method 'manually' within a try/catch/finally statement.
The Dispose() is designed to clear up 'unmanaged' resources used by an object such as file handles, database connections and the like as a prelude to the object itself being garbage collected.
It's preferable from a GC point of view to use the 'using' statement rather than calling Dispose() manually as this ensures that the object (if declared at the head of the statement) will automatically go out of scope when the 'using' statement ends and will become eligible for garbage collection without any delay.
Otherwise you have to remember to call Dispose() and then set the object variable to null manually. Failure to take these steps may lead to a resource leak or to the object hanging around in memory longer than it needs to.