What is the use of using statement in C#
Sumit Jolly
there is 2 use using statement. 1:- Importing namespace example:using System; 2:- dispose the object using(SqlConnection oConnection=new SqlConnection()) { oConnection.Open(); } if you won't close the connection it will close the connection automatically once SqlConnection has been executed completing.
using statement to release all the resources automatically.
using statement is use to implement all classes and objects ,method which are define in a namespace
For import the Namespace for Close the Connection to database
We use using statement to release all the resources automatically.
The reason for the "using" statement is to ensure that the object is disposed as soon as it goes out of scope, and it doesn't require explicit code to ensure that this happens.
Auto dispose the objects ,when the execution of block completed. and Importing Namespaces
A class which inherits from IDisposable interface and also defines Dispose method for releasing resources from memory.The same class can be used in "Using" statement.
For importing namespaces.
using statement is used to import the library. In C language, #include is such a header file which imports the library and under the library various function works. using statement is the same thing. example: using system.data.sqlclient; this imports library of some ado.net methods
It is used to define the statement
It's a syntax to include system file ot import namespace
1. Referencing Namespace 2. Deallocting objects which has implemented IDisposable Interface
If a class implements IDisposable then it will create some unmanaged resources which need to be 'disposed' of when you are finished using them.http://stackoverflow.com/questions/75401/uses-of-using-in-c-sharp
if we r use the using then it automatically release the objects from the memory
The using statement is mostly used when you need to one or more resources in a segment. The using statement obtains one or various resources, executes them and then releases the objects or resources. It is widely used in database connectivity through C#.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 }
When you are using an object that encapsulates any resource, you have to make sure that when you are done with the object, the object's Dispose method is called. The using statement simplifies the code that you have to write to create and then finally clean up the object.
The using statement automatically calls the Dispose() method on the object when leaving the scope of the using statement. using statement is more elegant way of disposing object than manual dispose
The using statement simplifies the code that you have to write to create and then finally clean up the object. The using statement obtains the resource specified, executes the statements and finally calls the Dispose method of the object to clean up the object.
The using block is used to obtain a resource and use it and then automatically dispose of when the execution of block completed.