using System; using System.Threading; namespace DemoInheritanceInterfaces { public class Person : IDisposable { private string name; private double salary; private bool disposed = false; // Constructor. public Person(string n, double s) { name = n; salary = s; Console.WriteLine("In constructor for {0}.", name); } // Allow explicit disposal. public void Dispose() { if (!this.disposed) { // Release resources... Console.WriteLine("In Dispose() for {0}.", name); // Set a flag to deal with multiple disposals. this.disposed = true; // Suppress destructor for this object. GC.SuppressFinalize(this); } } // Destructor. ~Person() { Console.WriteLine("In destructor for {0}.", name); this.Dispose(); } } static class ObjectDisposalDemo { public static void DoDemo() { Console.WriteLine("\nObjectDisposalDemo"); Console.WriteLine("------------------------------------------------------"); DisposeExplicitly(); DisposeWithUsing(); } public static void DisposeExplicitly() { // Create objects. Person obj1 = new Person("John", 50000); Person obj2 = new Person("Jane", 60000); // Explicitly unreference an object. Console.WriteLine("Unreferencing John."); obj1 = null; Console.WriteLine("Sleeping for 3 seconds..."); Thread.Sleep(TimeSpan.FromSeconds(3)); // Explicitly dispose an object Console.WriteLine("Calling Dispose() on Jane."); obj2.Dispose(); Console.ReadLine(); } public static void DisposeWithUsing() { Console.WriteLine("\nAbout to enter a using block."); // Declare a variable in a 'using' clause. using (Person obj = new Person("Fred", 50000)) { Console.WriteLine("Making use of Fred in using block."); } // Dispose() called automatically here. Console.WriteLine("After using block."); Console.ReadLine(); } } }