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.
views:
241answers:
10Unfortunately 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.
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.
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.
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.
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.
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.
start typing and see if intellisense picks up a .Dispose method
(as per comments, is quick and (very) dirty)
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.
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.
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!