Interviews - C# Corner

Manish Rohilla
What are the Two use of an Using Statement in c#?
By Manish Rohilla in ASP.NET onOct 31 2013
  • Roymon TP
    Oct, 2014 9

    1)for name space inclusion 2) for code clean up.

    • 0
  • Rajesh Kumar Jha
    Nov, 2013 14

    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 :)

    • 0
  • Manish Rohilla
    Oct, 2013 31

    • 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.

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS