What are the uses of “using” in C#?

后端 未结 29 2972
有刺的猬
有刺的猬 2020-11-21 07:31

User kokos answered the wonderful Hidden Features of C# question by mentioning the using keyword. Can you elaborate on that? What are the uses of

29条回答
  •  星月不相逢
    2020-11-21 07:47

    Using Clause is used to define the scope for the particular variable. For example:

         Using(SqlConnection conn=new SqlConnection(ConnectionString)
                {
                    Conn.Open()
                // Execute sql statements here.
               // You do not have to close the connection explicitly here as "USING" will close the connection once the object Conn becomes out of the defined scope.
                }
    

提交回复
热议问题