tags:

views:

252

answers:

8

Will the following code work if resource doesn't implement IDisposable?

  T resource = new T();
  using (resource as IDisposable)
  {
     ...
  }
A: 

If by "will it work" you mean will you get a runtime error? No, you won't.

casperOne
+8  A: 

Yes, it will work, check this running test:

[TestMethod]
public void TestMethod8()
{
    using (new MyClass() as IDisposable)
    {
    }
}
public class MyClass { }

It just runs without any issue. If the corresponding class implements IDisposable it will call it, if not it will still work/run :).

Update: As others have said, I also wonder what's the use you want to give to it. My guess is you have something like a factory that can get instances from different classes, which may or may not be disposable :).

eglasius
+1  A: 

What's the use of this ? What's the advantage ? What's the benefit ?

The using statement will make sure that the Dispose method is called, but if your object doesn't implement IDisposable, I see no use of doing this ? It will work (as in, it won't throw runtime exceptions and it will compile), i've just tested it here but ...

With the code you've written down, you can't even make use of the resource as being an IDisposable ...

Frederik Gheysels
@Frederik added to my answer: my guess is a factory that can get you instances from different classes which may or may not be disposable.
eglasius
A: 

I believe the compiler does add checks to determine if it is null before attempting to call Dispose.

http://aspnetresources.com/blog/the_very_handy_using_statement.aspx

+9  A: 

Yes. A using statement checks whether it's being given null and avoids trying to call Dispose if so.

From section 8.13 of the C# 3 spec:

A using statement is translated into three parts: acquisition, usage, and disposal. Usage of the resource is implicitly enclosed in a try statement that includes a finally clause. This finally clause disposes of the resource. If a null resource is acquired, then no call to Dispose is made, and no exception is thrown.

Jon Skeet
+1  A: 

It should work fine, even if isn't the "right" approach to disposing of resources. The using statment is just syntactic sugar for

IDisposable o = ...;

try
{
  // ...
}
finally
{
  if (o != null)
    o.Dispose(true);
}

So if o never implemented IDisposable it would be null in the using statement and it would try to call Dispose on it.

Samuel
@Samuel added to my answer: my guess is a factory that can get you instances from different classes which may or may not be disposable.
eglasius
+1  A: 

On testing the code, it appears not to result in any compile- or run- time errors.

From the language definition (see section 8.13):-

If a null resource is acquired, then no call to Dispose is made, and no exception is thrown.

If the type is not convertible to IDisposible the as operator will translate resource to null, and the code encapsulated in the using block will execute perfectly fine, behaving precisely the same as if you hadn't enclosed it in the using block.

In short, the answer to your question is yes.

kronoz
+1  A: 

It can be useful when the type explicitly implements IDisposable in which case the Dispose method is not directly accessible on the type unless the type is casted to an IDisposable, Using the 'as' does a safe cast and ensures that Dispose is called. In most cases when the type explicitly implements IDisposable, it provides methods such as 'Close' which in turn calls Dispose

Abhijeet Patel