using在(可能)null对象上使用该语句是否安全?
请考虑以下示例:
class Test {
IDisposable GetObject(string name) {
// returns null if not found
}
void DoSomething() {
using (IDisposable x = GetObject("invalid name")) {
if (x != null) {
// etc...
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
是否保证Dispose只有在对象不为空时才会被调用,而且我不会得到一个NullReferenceException?