C# 使用块的用途是什么?

C# 使用块的用途是什么?,c#,using-statement,C#,Using Statement,在哪种情况下可以使用using block,其好处是什么 using (some code statement here) { //code here } 仅当使用实现的对象时,使用块才有用。(试着说5次)。它确保在这些对象超出范围后调用它们的dispose方法 using(SqlConnection con = new SqlConnection(this.connString)) { //do stuff here } //con.Dispose() will be ca

在哪种情况下可以使用using block,其好处是什么

using (some code statement here) 
{
    //code here
}

仅当使用实现的对象时,使用块才有用。(试着说5次)。它确保在这些对象超出范围后调用它们的dispose方法

using(SqlConnection con = new SqlConnection(this.connString))
{
    //do stuff here
} //con.Dispose() will be called for you automatically.

谢谢@Matt的回答