Using (special use in C-Sharp)
From eqqon
(Difference between revisions)
Revision as of 09:50, 21 November 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