User kokos answered the wonderful Hidden Features of C# question by mentioning the using
keyword. Can you elaborate on that? What are the uses of
In conclusion, when you use a local variable of a type that implements IDisposable
, always, without exception, use using
1.
If you use nonlocal IDisposable
variables, then always implement the IDisposable pattern.
Two simple rules, no exception1. Preventing resource leaks otherwise is a real pain in the *ss.
1): The only exception is – when you're handling exceptions. It might then be less code to call Dispose
explicitly in the finally
block.