What is happening when I close a form, which was opened using Show(), by using Dispose() instead of Close()? Can someone tell me in detail, what happening in the Dispose() method?
What is happening when I close a form, which was opened using Show() ,
Share
The basic difference between
Close()andDispose()is, when aClose()method is called, any managed resource can be temporarily closed and can be opened once again. It means that, with the same object the resource can be reopened or used. Where asDispose()method permanently removes any resource ((un)managed) from memory for cleanup and the resource no longer exists for any further processing.Or just a general statement. With the connection object calling
Close()will release the connection back into the pool. CallingDispose()will callClose()and then set the connection string to null.Some objects such as a Stream implement
IDisposablebut the Dispose method is only available if you cast the object to anIDisposablefirst. It does expose aClose()method though.I would always argue that you should call
Dispose()on any object that implementsIDisposablewhen you are through with the object. Even if it does nothing. The jit compiler will optimize it out of the final code anyway. If the object contains aClose()but noDispose()then callClose().You can also use the using statement on
IDispoableobjectsThis will call
Dispose()on the SqlConnection when the block is exited.