If I use something like:
using (OdbcConnection conn = new OdbcConnection(....)) { conn.open(); my commands and sql, etc. }
Do I have to do a conn.close(); or does the using statement keep me from doing that last call? Does it dispose of everything in the using block? For example, if I called other objects unlrelated would it dipose of those automatically also?
Thank you. I was unclear after reading about using on Microsoft’s site. I want to make sure I don’t have any memory leaks.
See the top bit of my other answer for How do I use the using keyword in C# for a little more information.
I should also mention that you can close (dispose) of the connection as soon as you are done with it to release the resource. The guidelines say that the caller should be able to repeatedly call the dispose method. The using block is essentially just a safety net and allows writing clearer code in most circumstances.
[Edit] e.g. multiple initialization in a using: initialize more than one object in the same using without having to nest using blocks if the objects are the same type:
Joel Coehoorn mentioned stacking, which is nesting but omitting the braces, much as you can omit the braces in a
for, orifstatement. The UI doesn’t reformat with an indent. I’d be curious what the IL looks like.It is an error to use put different objects in the same using
error CS1044: Cannot use more than one type in a for, using, fixed, or declaration statement.