using System;
public class MyClass : IDisposable
{
protected string name;
public MyClass(string name) { this.name = name; }
override public string ToString() { return name; }
~MyClass() { Dispose(); Console.WriteLine("~MyClass()"); }
public void Dispose()
{
Console.WriteLine("MyClass.Dispose()");
GC.SuppressFinalize(this);
}
}
public class SonOfMyClass : MyClass, IDisposable
{
public SonOfMyClass(string name) : base(name) { }
override public string ToString() {
return name;
}
~SonOfMyClass() {
Dispose();
Console.WriteLine("~SonOfMyClass()");
}
new public void Dispose()
{
base.Dispose();
GC.SuppressFinalize(this);
}
}
class DerivedDisposeApp
{
static void Main(string[] args)
{
DoSomething();
}
static void DoSomething()
{
SonOfMyClass s = new SonOfMyClass("Bar");
Console.WriteLine(s);
s.Dispose();
}
}
|