IDisposable

From eqqon

Revision as of 19:44, 28 October 2007 by Henon (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


interface IDisposable

Objects that implement IDisposable can be used in the using statement. After the block ends, the object's Dispose-method is guaranteed to be called. Note: The object's destructor is not guaranteed to be called immediately.

Example

using System;

public class Resource : IDisposable
{
	public void Dispose() 
	{
		Console.WriteLine("Disposed");
	}
	~Resource() {
		Console.WriteLine("~Resource");
	}
}

public class Program 
{
	public static void Main() 
	{
		using( Resource h = new Resource()) {
			Console.WriteLine("Inside using block");
		}
		Console.WriteLine("After using block");
	}
}
Program output
Inside using block
Disposed
After using block
~Resource