While programming over an existent class, I’ve noticed that someone has written something like this:
using(DisposableObject disp = new DisposableObject())
{
...
...
disp.Dispose()
}
And then I wonder: isn’t the using block enough to dispose an object? Could be there any way Dispose() can be useful in such case?
Cause it doesn’t make any sense to me…
In your case it is useless to use
disposeinsideusingbecause whenusingstatement’s scope ends it automatically callsdispose. That’s why you can only write objects which implementsIDisposableInterface insideusingbrackets.Moreover if there was any statement using
dispobject afterdisp.Dispose()it would give an error, because by then object would have been disposed, i.e. its memory has been released.But beware
If
disposeis last line beforeusingscope ends then it is useless.But it is not useless when there are more lines after
dispose.