The Dispose Pattern : IDisposable : Class Interface C# Source Code


Custom Search

C# Source Code » Class Interface » IDisposable »

 

The Dispose Pattern








    
 
using System;


public class MyClass {
    private string name;
    public MyClass(string name) { this.name = name; }
    override public string ToString() { return name; }
    ~MyClass() { Console.WriteLine("~MyClass()"); }
    public void Dispose() {
        Console.WriteLine("Dispose()");
    }
}

public class GarbageDisposalApp {
    public static void Main(string[] args) {
        DoSomething();
        Console.WriteLine("end of Main");
    }
    public static void DoSomething() {
        MyClass t = new MyClass("Foo");
        Console.WriteLine(t);
        t.Dispose();
        t = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Class Interface
» IDisposable