Net-informations.com

.Net Frequently Asked Questions (C#, Asp.Net and VB.Net)

The following sections contains Frequently Asked Questions (Latest and authentic Interview questions) about Microsoft .NET Framework and its related technologies including Asp.Net, C# and VB.Net.

What is the use of using statement in C# ?

c# using statement

Placing your code inside a using block ensures that it calls Dispose() method after the using-block is over, even if the code throws an exception.


Example:



is same as



C# Using Statement Calls Dispose

c# dispose

In the above code blocks, the second code block is uses the "using" statement, so in this case the objects are disposed as soon as control leaves the block. It provides a convenient syntax that ensures the correct use of IDisposable objects, also we can see implementing using is a of way shorter and easier to read.

Working Example



Using using statement



c#  statement

Here we can understand using block is used to acquire a resource and use it and then it automatically dispose of when the execution of block completed. Here we can see it simplifies the code that you have to write to create and then finally clean up the Object. More..... Frequently Asked Questions