nametmp

导航

对using的新认识

以前一直不知道using(SqlConnection myConnection = new SqlConnection(connectString)) {  } 中using的实现细节,不过模糊知道限定myConnection只能在大括号中起作用,拜读了《c#对于如何释放资源的解释,又让我对此语言有更进一步的理解》后,才明白这个using是等于try、catch+Dispose(using的对象须支持IDispose接口)。

using(SqlConnection myConnection = new SqlConnection(connectString))
{
     myConnection.Open();
}

等效与:
try
{
      SqlConnection myConnection 
= new SqlConnection(connectString);
      myConnection.Open();
}

finally
{
      myConnection.Dispose();
}

posted on 2008-06-08 17:51  nametmp  阅读(212)  评论(1编辑  收藏  举报