Managing Resources in .NET
A simple tip for managing your resources smartly with 'using'
using (SqlConnection sqlConn =
new SqlConnection(...))
{
sqlConn.Open();
using (SqlCommand sqlCmd =
new SqlCommand(..., sqlConn))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
// Access elements items here
}
}
}
Extract from MSDN:
C#, through the .NET Framework common language runtime (CLR), automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible.