I just had a small discussion about the "Using"-Keyword. A topic that everybody seems to have a slightly different, sometimes evan magical, view on. But what does it really do?
The "Using" keyword makes sure if an exception occurs during the life of an objectinstance or when that objectinstance goes out of the using-scope (thanks Edward Bakker) that the IDisposble.Dispose() method is called.
Nothing more, nothing less. That's all folks.
Because the IDisposble-interface is mostly implemented on objects that use managed resources it is often thought that it the "using"-keyword magically cleans up these resources but that is clearly not the case nor does it magically close open databaseconnections for example!
As a rule of thumb, you might want to use the "Using"-keyword for all objects that implement the IDisposable interface. Because this can be easyilly forgotton and because this should be part of every codereview this is an excellent candidate for a "CodeAnalysis"-rule. If it not allready is?
Happy cleaning!
Wednesday, May 7, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment