Is it safe to use the using
statement on a (potentially) null object?
Consider the following example:
class Test {
IDisposable GetObject(string name) {
// returns null if not found
}
void DoSomething() {
using (IDisposable x = GetObject("invalid name")) {
if (x != null) {
// etc...
}
}
}
}
Is it guaranteed that Dispose
will be called only if the object is not null, and I will not get a NullReferenceException
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…