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 method 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.
For eg.
using (SqlConnection conn = new SqlConnection())
{
}
//When we use above block, internally the code is generated like this
SqlConnection conn = new SqlConnection()
try
{
}
finally
{
// calls the dispose method of the conn object
}
Using statement make the code more readable and compact.
IDisposable interface has one public method 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.
For eg.
using (SqlConnection conn = new SqlConnection())
{
}
//When we use above block, internally the code is generated like this
SqlConnection conn = new SqlConnection()
try
{
}
finally
{
// calls the dispose method of the conn object
}
Using statement make the code more readable and compact.
Comments
Post a Comment