Friday 9 December 2016

Using


USING BLOCK


Generally in our applications we will write code like create connection object to handle connectionstring after that open a connection and create command object etc. to interact with database to get data that would be like this 


It’s very easy way to write above code but problem is SqlConnection and SqlCommand objects will create IDISPOSABLE interface that means it could create unmanaged resources in our application to cleanup those objects we need to call Dispose() method at the end of our process otherwise those objects will remain in our application.

Suppose we use using statement in our applications it will automatically create try / finally blocks for the objects and automatically runs Dispose() method for us no need to create any try/finally block and no need to run any Dispose() method.

If we use using statement we need to write code will be like this 

C# Code



In above code using statement no need to worry about close connection because using statement automatically close the connection once process is complete and automatically it will run Dispose() method to dispose objects. 

The above using statement code will equal to below code








Thank you for taking your valuable time look at my blog - Sadiq Ali Syed




No comments: