Interviews3 answers
What are the Two use of an Using Statement in c#?
3 Answers
1)for name space inclusion
2) for code clean up.
Roymon TPPosted Oct 8, 2014, 10:57 PM EDT
I wish to talk more about the Using Clause.
You can Use Using with all the objects that Implements IDisposable Interface, SO when you get out of the Scope of 'Using' , It Disposes the Object and it should not be further Used.
This is a best known practice to do so :)
Rajesh Kumar JhaPosted Nov 13, 2013, 8:02 PM EST
- To Import a Namespace. Example Using System.Data;
- To close the connection in ADO.Net
string cs = ConfigurationManager.ConnectionStrings["ADO"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("select * from sample", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
}
Here above you can see that , i didn't use con.Close() method. Because i used a Using method above.
Manish RohillaPosted Oct 31, 2013, 8:10 AM EDT
