Which is better, and when: using statement or calling Dispose() on an IDisposable in C#?
.net, asp.net, c#, idisposable, using-statement
Solution
The first is better. It ensures it is disposed even if an exception is thrown, and it correctly handles the case where `Create(0)` returns null (i.e. it doesn't attempt to call `Dispose()` on a null instance).
Problem
Suppose I have the following: ``` using(var ctx = DataContextFactory.Create(0)) { ... Some code ... } ``` Why not just do the following and lose a couple of curly braces?: ``` var ctx = DataContextFactory.Create(0); ctx.Dispose(); ``` Thanks for the advice!