EPiWiki.se  - EPiServer notes shared with others
 

IDisposable pattern

[Edit]
When a class implements IDisposable, the correct way of using that class is to use the
reserved world “using “ (or call the Dispose method).
Why the dispose pattern dispose the object and executes the method
GC.SuppressFinalize that tells the object not to place itself in the destructor queue,
if a object that is hard to destroy it stops the destructor queue and no other objects
are destroyed from that queue wish can souse an “Out of memory exception”

Example


The correct usage of IDisposable pattern

using (SqlConnection connection = new SqlConnection(connectionString))
{
   ...
}

Don't do this

try
{
   SqlConnection connection = new SqlConnection(connectionString);
   ...
} finnlly
{
   connection.Close();
}

Because the second example close the connection but, the objects destructor has to be executed by the finalizer queue (and it's har to know what the Dispose method does with the object maybe more then close it).
Version author:
Mattias Lövström

EPiServer version

All