215

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?

StayOnTarget
13.3k11 gold badges65 silver badges115 bronze badges
asked Mar 26, 2010 at 11:22
2

6 Answers 6

207
Kissaki
9,2955 gold badges43 silver badges45 bronze badges
answered Mar 26, 2010 at 11:26

3 Comments

Note that even if your variable is null, the using block is executed, and if you reference your variable inside the using block without first null-checking it, you WILL get NullReferenceException. To prevent misinterpretation, this answer should state: "Yes, Dispose() is only called on non-null objects".
The page referenced by the link doesn't mention (anymore?) what happens when the object is null.
49

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();
 }
}
Andriy Volkov
19k9 gold badges70 silver badges85 bronze badges
answered Mar 26, 2010 at 11:25

Comments

22

You should be ok with it:

using ((IDisposable)null) { }

No exception thrown here.

Side note: don't mistake this with foreach and IEnumerable where an exception will be thrown.

answered Mar 26, 2010 at 11:25

Comments

2

Yes, before Disposing the reference will be null-checked. You can examine yourself by viewing your code in Reflector.

answered Mar 26, 2010 at 11:27

Comments

0

I came across this question as I had an object where I don't know whether it requires and supports disposing. The way I dealt with this is to cast it to IDisposable using the as keyword. Here's an example:

void DoSomethingWithMyType(Func<IMyType> factory)
{
 IMyType myObj = factory();
 // Some but not all implementations IMyType of implement IDisposable
 using (myObj as IDisposable)
 {
 myObject.DoSomething();
 }
}

In my example scenario IMyType is an interface that doesn't implement IDisposable itself, but some classes that implement IMyType are known to implement IDisposable while others do not. In the former case, the myObj as IDisposable expression produces an IDisposable object on which on implicit call to Dispose is made leaving the using block. In the latter case, the expression produces null, in which case nothing happens, and crucially, no exception is raised when leaving the using block.

answered Apr 2, 2024 at 13:28

Comments

-3

You will not get null reference exception as per my experience. It will be simply ignored.

answered Mar 26, 2010 at 11:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.