views:

624

answers:

9

I was wondering if there was some sort of cheat sheet for which objects go well with the using statement... SQLConnection, MemoryStream, etc.

Taking it one step further, it would be great to even show the other "pieces of the puzzle", like how you should actually call connection.Close() before the closing using statement bracket.

Anything like that exist? If not, maybe we should make one.

+1  A: 

With ReSharper you can show all derived types. Maybe you can do it with the object browser without ReSharper, too. Go to the interface definition and look for "show inheritors".

tanascius
Nor knowing ReSharper, does it work for interfaces? And is it Project-wide or could you include the GAC and other folders?
Henk Holterman
It works for everything, and can include library assemblies, including those in the GAC. You can also click in an assignment, and it will suggest a using block if the type being assigned implements IDisposable.
John Saunders
+2  A: 

If you are unsure whether a class implements IDisposable or not, enclose it in a using block regardless. If you get a compile error, just remove it. You'll only lose a few seconds typing time.

Christian Hayter
True, this does work like a charm.
SkippyFire
In most cases, if there is a Dispose method it will implement IDisposable.
@weiqure - but not vice versa
Henk Holterman
Always vice versa as there is no IDisposable-implementing class without a Dispose method. And I have not yet seen a class that has a Dispose method but does not implement IDisposable.
+4  A: 

Microsoft FxCop has a rule checking that you use an IDisposbale in a using block.

Dmitry Risenberg
+2  A: 

An simple way to get a list of types that implement IDisposable is to crack open Reflector, navigate to System.IDisposable, expand the node, and then expand the 'Derived Types' node.

To be sure that your list is complete, verify that all the assemblies you're using have been 'Open'ed in Reflector.

Kevin Pullin
+3  A: 

The following C# method will list all IDisposable types found in a certain assembly. (Used namespaces: System, System.Collections.Generic, System.IO, System.Reflection)

  static void PrintDisposableTypesFromFile(String path)
  {
     Assembly assembly = Assembly.LoadFrom(path);
     foreach (Type type in assembly.GetTypes())
        if (type.GetInterface("IDisposable") != null)
           Console.WriteLine(type.FullName);
  }

The following C# method makes use of the previous one to do the same for all assemblies in a directory and its subdirectories, e.g. "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727":

  static void PrintDisposableTypesFromDirectory(DirectoryInfo dir, bool warn)
  {
     foreach (FileInfo file in dir.GetFiles("*.dll"))
     {
        try
        {
           PrintDisposableTypesFromFile(file.FullName);
        }
        catch (Exception ex)
        {
           if (warn)
           {
              Console.Error.WriteLine(
                 String.Format(
                    "WARNING: Skipped {0}: {1}",
                    new object[] { file.FullName, ex.Message }));
           }
        }
     }
     // recurse
     foreach (DirectoryInfo subdir in dir.GetDirectories())
        PrintDisposableTypesFromDirectory(subdir, warn);

  }

I'm not sure the list of all disposables is very useful, but I've used similar code to find other interesting things like the full list of text encodings supported by the .NET framework.

Wim Coenen
+1 for effort, but I think it would be easier to just use reflector :)
SkippyFire
A: 

A quick&dirty way of checking if a type implements IDisposable is to create an instance of it and check if the instance has a Dispose() member function. If it does then you can be 99% sure that it implements IDisposable.

Rune Grimstad
+3  A: 

In addition to the other answers, note that a class might implement IDisposable but not have Dispose come up in the intellisense list.

class MyClass :IDisposable
{
    void IDisposable.Dispose()
    {
        /* Stuff */
    }
}
billpg
+2  A: 

If you are using Visual Studio you can press F12 on a type declaration it will take you to a meta-data screen or the class definition (if you have the source code). If you keybindings are different right-click and "go to definition". From there you can see what a class implements. I suggest doing this for all classes the first time you encounter them to get a "feel" for what the class can do.

Kleinux
Yes but not every class that implements IDisposable implements it directly. Take DataTable for example (which, BTW, you don't need to Dispose... but that's another conversation http://stackoverflow.com/questions/913228/should-i-dispose-dataset-and-datatable.)
Grinn
+1  A: 

Perhaps glance at my post on this at http://www.lancemay.com/2010/01/idisposable-cheat-sheet/. Not sure if that's what you're looking for, but based on the original question, it sounds like it may be.

Lance May
Cool! This is exactly what I was originally looking for!
SkippyFire