Interviews5 answers
Which is better, and when: using statement or calling Dispose() on an IDisposable in C#?
5 Answers
Using "using" statement because it will automatically call dispose when cursor moves out of that block at runtime.
Vishal JadavPosted Aug 15, 2016, 2:52 AM EDT
Using should be used whereever applicable. It will automatically dispose off once program is out of the block
sanjib kotalPosted Jul 9, 2016, 4:53 AM EDT
I prefer the "using" statement over manual Dispose.
Sachin KaliaPosted Apr 22, 2014, 9:08 AM EDT
I agree. I prefer the "using" statement over manual Dispose.
Mahesh ChandPosted Jul 12, 2012, 5:20 AM EDT
A 'using' statement is always better because
a) you can't forget to call Dispose, even as the code evolves into different code paths, and
b) Dispose gets called even if there's an exception. It also checks for null before calling Dispose, which may be useful.
The only place you don't want to use a using block is where the disposable object is scoped outside of the function. In this case, your class should implement IDisposable and dispose of the object in its Dispose()
Murali PoolaPosted Apr 23, 2012, 12:47 AM EDT


