using ; . "" .
( using ) - , FileStream.Close , . , . , , Close finally.
, , try/catch/finally try/using/catch. , finally, - , IDisposable, . :
try {
using (DisposableObject obj1 = GetDisposableObject()) {
using (DisposableObject obj2 = GetAnotherDisposableObject()) {
using (DisposableObject obj3 = GetYetAnotherDisposableObject()) {
}
}
}
} catch (SomeException ex) {
}
:
DisposableObject obj1 = null;
DisposableObject obj2 = null;
DisposableObject obj3 = null;
try {
obj1 = GetDisposableObject();
obj2 = GetAnotherDisposableObject();
obj3 = GetYetAnotherDisposableObject();
} catch (SomeException ex) {
} finally {
if (obj3 != null) obj3.Dispose();
if (obj2 != null) obj2.Dispose();
if (obj1 != null) obj1.Dispose();
}
.
, . .