Using Statement in C#


Ever stumbled across a code snippet as follows
public static DataSet GetMissingStuff()
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["HELLO"]))
            {
                conn.Open();

                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "[jupiter].[get_Missing_Responses]";
                    cmd.CommandType = CommandType.StoredProcedure;

                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        return ds;
                    }
                }
            }

        }

 And ever thought about making use of Using Statement in the following fashion.
Reason to use this : We know that every object created in C# needs to be disposed  off  after been used .Well the using statement does that for us , so as to we need not bother about it been disposed.

A using statement is translated into three parts: acquisition, usage, and disposal.

No comments:

Post a Comment