0

In which situation can a using block be used and what are the benefits?

using (some code statement here) 
{
    //code here
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Deepak
  • 1,510
  • 1
  • 14
  • 27

1 Answers1

2

Using blocks are only useful when utilizing objects that implement IDisposable. (Try saying that 5 times fast). It ensures that the dispose methods of those objects are called after they fall out of scope.

using(SqlConnection con = new SqlConnection(this.connString))
{
    //do stuff here
} //con.Dispose() will be called for you automatically.
Matt
  • 3,638
  • 2
  • 26
  • 33