












以前一直不知道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();
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。