Click here to Skip to main content
15,885,981 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Codeproject

Can anyone explain it to me why

Using either the Close method or the Dispose method is sufficient. You do not have to call one method after the other. There is no benefit to calling one method after the other.
[^]

Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 13-Nov-13 12:02pm    
Closing and Disposing of what?! So far, the question makes no sense. These concepts are not directly related, and, generally, one does not replace another.
—SA

Because in this case, Close and Dispose do the same thing as far as any used resources are concerned - they both release them (There is a good chance that Dispose calls Close, or vice versa)
An implementation of Dispose is required by all classes that implement IDisposable (and thus can be instantiated in a using block) but there is nothing stopping classes (such as Streams for example) from adding the same functionality under a different name - and it is more symmetrical to have code that does this:
C#
myStream.Open();
   ...
myStream.Close();
rather than
C#
myStream.Open();
   ...
myStream.Dispose();
 
Share this answer
 
For database connections, there's a small difference between both.
Dispose will release the database connection completely, so it can't be reopened.
Close will close the connection but will keep the connection string, connection timeout etc intact, so you can reopen it.

But there's no benefit in closing then disposing, as dispose already does the close.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900