Finalizing Objects

Unlike many other programming languages (C, C++, Pascal, and so on), C# provides garbage collection. Your objects are destroyed after you are done with them—although not immediately after; they’re destroyed when the garbage collection process runs, which is determined by the system. You do not need to worry about cleaning up after your objects unless you use unmanaged or scarce resources. An unmanaged resource is an operating-system feature outside the .NET Framework, such as a connection to a database. A scarce resource is a resource that you have in limited quantity, perhaps because of licensing limitations or limited bandwidth. Graphics resources, such as fonts and brushes, are considered scarce because of the way the operating system works.

If you do control an unmanaged resource, you need to explicitly free that resource when you are done with it. Typically, you’ll manage this by implementing the IDisposable interface. (You will learn more about interfaces in Chapter 13.)

The IDisposable interface requires you to create a method named Dispose( ), which will be called by your clients.

If you provide a Dispose( ) method, you should stop the garbage collector from calling your object’s destructor. To stop the garbage collector, call the static method GC.SuppressFinalize( ), passing in the this reference for your object. Your finalizer can then call your Dispose( ) method. Thus, you might write:

using System;
class Testing : IDisposable
{
   bool is_disposed = false;
   protected virtual void Dispose( bool disposing )
   {
      if ( !is_disposed ) // only dispose once!
      {
         if ( disposing )
         {
            // OK to reference other objects
         }
         // perform cleanup for this object
         Console.WriteLine( "Disposing..." );
      }
      this.is_disposed = true;
   }
    public void Dispose( )
   {
      Dispose( true );
      GC.SuppressFinalize( this );
   }
}

For some objects, you’d rather have your clients call a Close( ) method because that is the keyword they use historically (such as File.Close( )).You can implement this by creating a private Dispose( ) method and a public Close( ) method and having your Close( ) method invoke Dispose( ).

Because you cannot be certain that your user will call Dispose( ) reliably, and because finalization is nondeterministic (that is, you can’t control when the garbage collector will run), C# provides a using statement to ensure that Dispose( ) is called at the earliest possible time. The idiom is to declare which objects you are using and then to create a scope for these objects with curly braces. When the close brace is reached, the Dispose( ) method will be called on the object automatically, as illustrated here:

using System.Drawing;
class Tester
{
 public static void Main( )
 {
     using (Font theFont = new Font("Arial", 10.0f))
     {
     // use the font
     }
 }
}

The keyword using is overdetermined—that is, it is used in two ways in C#. The first way is to indicate that you are using a namespace, as you see in the preceding code snippet:

using System.Drawing

The second way is in the using statement that creates a scope to ensure finalization, as you see in this line:

using (Font theFont = new Font("Arial",10.0f))

Because Windows lets you have only a small number of Font objects, we want to dispose of it at the earliest opportunity. In this code snippet, the Font object is created within the using statement. When the using statement ends, Dispose( ) is guaranteed to be called on the Font object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset