IDisposable

From eqqon

(Difference between revisions)
Jump to: navigation, search
Henon (Talk | contribs)
(New page: Category:CSharp = interface IDisposable = Objects that implement '''IDisposable''' can be used in the '''using''' statement. After the block ends, the object's '''Dispose'''-method is...)

Latest revision as of 19:44, 28 October 2007


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