考虑以下示例。
public void SomeMethod(){
using(var sqlConnection= new SQLConnection()){
//some code here
}
}
在上面的例子中, using 块之外的 sqlConnection 将被释放并收集垃圾
public void SomeMethod(){
var sqlConnection = new SQLConnection(){
}
}
在此示例中,sqlConnection
对象将在SomeMethod()
.
这里的问题是,在这种情况下是否真的有必要使用using()
范围,因为我可以在执行结束时收集对象垃圾。有人可以在这里分享您的想法。