Wednesday, 25 January 2017

What is the use of Using Keyword in C#


Using statement is used to work with an object in c# that inherits IDisposable interface.
IDisposable interface has one public methoda called Dispose that is used to dispose off the object.When we use Using
statement,we don't need to explicitly dispose the object in the code, the using statement takes care of it.

using(SqlConnection conn= new SqlConnection)
{
}

When we use above code block,internally the code is generated like this

SqlConnection conn= new SqlConnection()


try
{

}
finally
{
   //calss the dispose method of the connection object.
}

No comments:

Post a Comment