C#
  Home arrow C# arrow Page 2 - C# Tips: Keep it Short, Get it Done
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# Tips: Keep it Short, Get it Done
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 24
    2006-01-30

    Table of Contents:
  • C# Tips: Keep it Short, Get it Done
  • Implementing IDisposable
  • Using “using”
  • Using “as” and “is"

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C# Tips: Keep it Short, Get it Done - Implementing IDisposable


    (Page 2 of 4 )

    Generally speaking, you do not need to worry about disposal. The main exception, however, is when you are using an unmanaged resource – like a database connection or a flat file – or any other resource, except for memory. The .NET framework provides the “IDisposable” interface for this purpose. Since there is no way to guarantee that the “Dispose()” method gets called, it is considered a best practice to wrap a call to the “Dispose()” method in an objects finalizer (destructor).

    When the garbage collector runs, all objects with a finalizer are left in memory and have their finalizers executed. Objects without finalizers are simply deleted from memory – which is where unmanaged resources pose the potential for leakage.

    Here is the “IDisposable” interface:

    public interface IDisposable
    {
         void Dispose();
    }

    The following code illustrates the basic usage of the “IDisposable” interface.

    public class MyClass : IDisposable
    {
         private bool _isDisposed = false;

         ~MyClass()
         {
              Dispose(false);
         }

         public void Dispose()
         {
              Dispose(true);
              GC.SuppressFinalize(true);
         }

         protected virtual void Dispose(bool isDisposing)
        {
              if (_isDisposed)
                   return;
              if (isDisposing)
             {
                  // Free managed resources
              }

             // Free unmanaged resources here
            _isDisposed = true;
        }
    }

    You may be wondering why there is a virtual overloaded version of the “Dispose()” method in the code above. We created the virtual method to be used as a hook for derived classes to call the base classes disposal method, thus making sure that all resources are properly released.

    The most important thing to remember when implementing disposal is that you should only be freeing resources in the “Dispose()” method, nothing else. Do not call any object methods or create references to the current object or do anything else that could effectively resurrect that object. Remember, you never know in what order objects will be disposed, so you may end up working on an object that is already disposed or finalized. Doing that puts the object in question back into a global list of objects, but the garbage collector will not know to dispose of it again because it’s already been done!

    More C# Articles
    More By David Fells


       · Hello! Thanks for checking out my article. Your comments and feedback are welcome,...
       · One important tip: Don't overuse the "as" operator.For example, if myObject is...
     

    C# ARTICLES

    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT