【发布时间】:2010-03-26 11:22:44
【问题描述】:
在(可能的)空对象上使用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?
【问题讨论】:
标签: c# idisposable using