Is there a list of common object that implement IDisposable for the using statement?

前端 未结 9 2377
挽巷
挽巷 2020-12-17 15:10

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

相关标签:
9条回答
  • 2020-12-17 15:59

    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.

    0 讨论(0)
  • 2020-12-17 16:00

    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.

    0 讨论(0)
  • 2020-12-17 16:02

    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 */
        }
    }
    
    0 讨论(0)
提交回复
热议问题