Dispose
public static class DisposeSample {
public static void Execute() {
using (var obj = new DerivedClass()) {
}
}
class BaseClass : IDisposable {
private bool _disposed = false;
~BaseClass() {
Dispose(disposing: false);
}
public void Dispose() {
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (_disposed) return;
if (disposing)
{
}
Console.WriteLine($"BaseClass.Dispose({disposing});");
_disposed = true;
}
}
class DerivedClass : BaseClass {
private bool _disposed = false;
protected override void Dispose(bool disposing) {
if (_disposed) return;
if (disposing)
{
}
Console.WriteLine($"DerivedClass.Dispose({disposing});");
base.Dispose(disposing);
_disposed = true;
}
}
}
Execute()実行結果
DerivedClass.Dispose(True);
BaseClass.Dispose(True);