Close and Dispose - which to call?

I want to clarify this situation.

According to Microsoft guidelines, it's a good practice to provide Close method where suitable. Here is a citation from Framework design guidelines

Consider providing method Close(), in addition to the Dispose(), if close is standard terminology in the area. When doing so, it is important that you make the Close implementation identical to Dispose ...

In most of cases Close and Dispose methods are equivalent. The main difference between Close and Dispose in the case of SqlConnectionObject is:

An application can call Close more than one time. No exception is generated.

If you called Dispose method SqlConnection object state will be reset. If you try to call any method on disposed SqlConnection object, you will receive exception.

That said:

  • If you use connection object one time, use Dispose. A using block will ensure this is called even in the event of an exception.
  • If connection object must be reused, use Close method.

For SqlConnection, from the perspective of the connection itself, they are equivalent. According to Reflector, Dispose() calls Close() as well as doing a few additional memory-freeing operations -- mostly by setting members equal to null.

For Stream, they actually are equivalent. Stream.Dispose() simply calls Close().


As usual the answer is: it depends. Different classes implement IDisposable in different ways, and it's up to you to do the necessary research.

As far as SqlClient goes, the recommended practice is to do the following:

using (SqlConnection conn = /* Create new instance using your favorite method */)
{
    conn.Open();
    using (SqlCommand command = /* Create new instance using your favorite method */)
    {
        // Do work
    }
    conn.Close(); // Optional
}

You should be calling Dispose (or Close*) on the connection! Do not wait for the garbage collector to clean up your connection, this will tie up connections in the pool until the next GC cycle (at least). If you call Dispose, it is not necessary to call Close, and since the using construct makes it so easy to handle Dispose correctly, there is really no reason to call Close.

Connections are automatically pooled, and calling Dispose/Close on the connection does not physically close the connection (under normal circumstances). Do not attempt to implement your own pooling. SqlClient performs cleanup on the connection when it's retrieved from the pool (like restoring the database context and connection options).

*if you are calling Close, make sure to do it in an exception-safe way (i.e. in a catch or finally block).

Tags:

.Net