在下面的示例中,如果在using语句中引发异常,则连接是否会关闭并处理?
using (var conn = new SqlConnection("..."))
{
conn.Open();
// stuff happens here and exception is thrown...
}
Run Code Online (Sandbox Code Playgroud)
我知道下面这段代码会确保它确实如此,但我很好奇使用声明是如何做到的.
var conn;
try
{
conn = new SqlConnection("...");
conn.Open();
// stuff happens here and exception is thrown...
}
// catch it or let it bubble up
finally
{
conn.Dispose();
}
Run Code Online (Sandbox Code Playgroud)