Cluj München 1140km

Gânduri clujene din Bavaria

München - Munich - Monaco di Baviera

5. martie 2009 17:20
by skorpionking
0 Comentarii

.NET Understanding IDisposable and Using Statement

5. martie 2009 17:20 by skorpionking | 0 Comentarii

The System.IDisposable interface is a very useful interface to understand if you are concerned about performance in your application. Microsoft says the following about the IDisposable interface:

"The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used, however, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams."

Most System.Data, System.IO and System.Windows.Controls objects use the IDisposable interface, as well as many others, to free up unmanaged resources that may have been created when the object was initialized. Unmanaged resources are any calls that are made outside of the .NET environment, this can be GDI+ calls, SQL Driver calls, Disk IO calls, or basically anything that cannot be accounted for by the Garbage Collector. You should always implement IDisposable when your class contains references to unmanaged resources (e.g. file handles, database connections, GDI objects, etc) rather than just adding a Dispose method to your class. That way the consumer of your class can choose to use the try ... finally approach or the using() statement.

For example, the proper way to use a SQL Connection is the following:

using (SqlConnection conn = new SqlConnection("{connection string}")) {
        using (SqlCommand command = conn.CreateCommands()) {

                command.CommandText = "select * from SomeTable";

              // code to use the command


Here is an example how to implement the IDisposable in your class:

public class MyDisposableClass : IDisposable

       private bool m_blnIsDisposed = false;  
        // Constructor
        public MyDisposableClass() { }  
        // Destructor 
        // Implementation of IDisposable
        public void Dispose() 
        protected virtual void Dispose(bool blnDisposing) 
            if (!m_blnIsDisposed)  
                // the method is called for the first time?
                    /* Release managed objects, when they implement 
                     * IDisposable call their Dispose() */           
                // Release unmananged objects (e.g. IntPtr)  
            // ensure that the method won't be call twice. 
            m_blnIsDisposed = true; 

And what does it have to do with using?

using (MyDisposableClass disThis = new MyDisposableClass())

       // do something with the instance disThis

The use of using will set the scope of this instance disThis. That means, as soon as the closing bracket } of the using statement will be reached, and the object is not used / referenced anymore, Dispose() will be called automatically. So implementing IDisposable and taking advantage of the using statement, you don't have to worry about calling the Dispose() method (It is important when you use for example some code that do tracing, use using to avoid surprizes).

Life is good :-) Happy coding!

Adaugă comentariu