在(可能)空對象上使用using
語句安全嗎?
請看下面的例子:對空對象使用語句
class Test {
IDisposable GetObject(string name) {
// returns null if not found
}
void DoSomething() {
using (IDisposable x = GetObject("invalid name")) {
if (x != null) {
// etc...
}
}
}
}
能夠保證所有的Dispose
只被調用,如果對象不是空的,我不會得到一個NullReferenceException
?
我建議嘗試一下......你已經得到了大部分你需要的代碼 – cjk 2010-03-26 11:24:33
相關http://stackoverflow.com/questions/2513035 – 2010-03-26 11:37:31