Yes, the best thing to do is using
. This code is equivalent to this:
{
FileStream fs = new FileStream("abc.txt", FileMode.Create);
try {
// Algum código...
} finally {
if (fs != null)
((IDisposable)fs).Dispose();
}
}
It roughly equates to try-resource
veloper .
In some cases, it may be more appropriate to do it manually, for example when you need a specific%% of this feature, or need to do something other than catch
in the dispose()
block. In these cases it is important to make the appropriate provision in finally
, as shown above.
Note that finally
has nothing to do with using
, as asked, but with try-catch
.