Derived Disposable Classes : IDisposable : Class Interface C# Source Code


Custom Search

C# Source Code » Class Interface » IDisposable »

 

Derived Disposable Classes








    
 
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();
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Class Interface
» IDisposable