Will Dispose() be called in a using statement with a null object?
Yes, Dispose()
is only called on non-null objects:
http://msdn.microsoft.com/en-us/library/yh598w02.aspx
The expansion for using
checks that the object is not null
before calling Dispose
on it, so yes, it's safe.
In your case you would get something like:
IDisposable x = GetObject("invalid name");
try
{
// etc...
}
finally
{
if(x != null)
{
x.Dispose();
}
}