/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example8_9.cs illustrates the use of the
the System.IDisposable interface and the using statement
to ensure fastest cleaning up as possible after an object
*/
using System;
// declare the Car class
class Car : System.IDisposable
{
// declare a field
public string make;
// implement the Dispose() method
public void Dispose()
{
Console.WriteLine("In Dispose()");
// do any cleaning up here
// stop the garbage collector from cleaning up twice
GC.SuppressFinalize(this);
}
// override the Finalize() method
~Car()
{
Console.WriteLine("In Finalize()");
// call the Dispose() method
Dispose();
}
}
public class Example8_9
{
public static void Main()
{
// create a Car object within the using statement
using (Car myCar = new Car())
{
// the Car object (and object reference) are only
// available within this block
myCar.make = "Toyota";
System.Console.WriteLine("myCar.make = " + myCar.make);
}
System.Console.WriteLine("At the end of Main()");
}
}