views:

241

answers:

10

In terms of coder productivity what is the quickest way in VS 2005 or 2008 to determine if Foo implements IDisposable. It has happened to me on more than one occasion that I've been caught using a type without a using block because it never occured to me that the thing would need to implement IDisposable.

+1  A: 

Unfortunately there's no "simple simple simple" way.

You're going to have to hit F12 on the class name, and then keep hitting F12 on base classes until you're in the base class, if any of those implement IDisposable, you've got it.

Lasse V. Karlsen
Thanks --- that's what I do currently, which prompted the question, becuase there really should be a better way.
Ralph Shillington
+1 for the shortcut I didn't know about
ck
+1  A: 

If the type is related to IO, windows/controls or graphics I usually take it as a hint that I should be looking for a Dispose method.

Also, the presence of a 'Close' method might imply that the type is disposable, e.g if you have a reference to an XmlReader (which is IDisposable) you can't see the Dispose method because it is implemented explicitly.

marklam
+6  A: 

If you've got ReSharper, highlight the variable, wait for the lightbulb, and then hit Alt+Enter. If one of the options is "Wrap with 'using'", it's IDisposable.

Chris Doggett
When doing class management, it really doesn't get much better than ReSharper and it has a cheap price entry point.
CAbbott
CodeRush has this too. The shortcut is CTRL+` on my system.
Dave Van den Eynde
In addition, I just found out, that CodeRush will mark this is an issue with orange curly underlining.
Dave Van den Eynde
@CAbbott: It's a bargain at twice the price. I shudder at the thought of developing C# on a machine without ReSharper.
Chris Doggett
+7  A: 

Put it in a using statement and see if it compiles:

using (var x = new TypeInQuestion(params)) {}

This won't compile if the TypeInQuestion doesn't implement IDisposable.

The other fastest way is to use ReSharper, click on the variable (x in this case), and see if it has a suggestion to put it in a using statement.

John Saunders
Downvote reason, please? I can't improve if I don't know what the problem was.
John Saunders
+1  A: 

The quickest way for me is usually to type a using block (using snippets) for an instance of the type in which case you will get an error notification if the type does not implement IDisposable. That usually takes just a couple of seconds. Not super convenient, but rather quick.

Fredrik Mörk
+1  A: 

In the most recent IDEs, if I have any question of if something implements IDisposible, I put it in a using block. The IDE will tell you if that is incorrect before you're even done with the next line of code.

AllenG
A: 

start typing and see if intellisense picks up a .Dispose method

(as per comments, is quick and (very) dirty)

SillyMonkey
Won't work if the type implements IDisposable explictly (you would need to cast first).
Richard
Won't work 100% of the time but it is a quick and dirty check; it also assumes that the author didn't add a Dispose method without implementing IDisposable.
Austin Salonen
+1  A: 

You can do this in PowerShell, e.g.

[System.Management.Automation.Runspaces.pipeline].getinterface("IDisposable")

returns

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    IDisposable

while

[datetime].getinterface("IDisposable")

returns nothing.

Richard
Not useful for custom types not loaded into Powershell.
Milan Gardian
Easy enough to add custom types into PowerShell: `[Reflection.Assembly]::Load(...)`.
Richard
+1  A: 

With the ReSharper plugin installed in Visual Studio, when you have your cursor on a variable declaration like on stringWriter in the snippet below:

TextWriter writer = new StringWriter();
writer.WriteLine("Test");

You can select "put into 'using' construct" from the 'lightbulb' menu, which will turn the above snippet into:

using (TextWriter writer = new StringWriter()) {
    writer.WriteLine("Test");
}

If the "put into..." option isn't there, the type doesn't implement IDisposable.

Ed Courtenay
A: 

Its not quick to do, but you can also run FXCop, and it will alert you to objects that are IDisposable that arent in a using() block.

Working on some legacy C# code here at work, I found that whoever wrote it orgininally actually made a ".Dispose()" method is almost every class, even if not needed, but the classes don't actually implement IDisposable. Which means in intellisence they all have a Dispose() method, but can't be put in using() blocks... very annoying!

rally25rs