Finalizable Disposable Class : Destructor « Class Interface « C# / C Sharp






Finalizable Disposable Class

 

using System;
using System.Collections.Generic;
using System.Text;

class Program {
    static void Main(string[] args) {
        using (MyResourceWrapper rw = new MyResourceWrapper()) {
        }
        MyResourceWrapper rw2 = new MyResourceWrapper();
        for (int i = 0; i < 10; i++)
            rw2.Dispose();
    }
}

public class MyResourceWrapper : IDisposable {
    public void Dispose() {
        Console.WriteLine("In Dispose() method!");
    }
}

 








Related examples in the same category

1.force the GC to invoke Finalize() for finalizable objects created in this AppDomain.
2.the destructors are called bottom-up, which confirms the sequencing of destructors.
3.Shows that stack unwinding in C# does not necessarily call destructorsShows that stack unwinding in C# does not necessarily call destructors
4.Illustrates a destructorIllustrates a destructor
5.Demonstrate a destructorDemonstrate a destructor