Dealing with .NET IDisposable objects

后端 未结 12 781
自闭症患者
自闭症患者 2021-01-30 13:14

I work in C#, and I\'ve been pretty lax about using using blocks to declare objects that implement IDisposable, which you\'re apparently always suppose

12条回答
  •  不知归路
    2021-01-30 13:47

    FxCop might help (although it didn't spot a test I just fired at it); but yes: you are meant to check. IDisposable is simply such an important part of the system that you need to get into this habit. Using intellisense to look for .D is a good start (though not perfect).

    However, it doesn't take long to familiarize yourself with types that need disposal; generally anything involving anything external (connection, file, database), for example.

    ReSharper does the job too, offering a "put into using construct" option. It doesn't offer it as an error, though...

    Of course, if you are unsure - try using it: the compiler will laugh mockingly at you if you are being paranoid:

    using (int i = 5) {}
    
    Error   1   'int': type used in a using statement must be implicitly convertible to 'System.IDisposable'    
    

提交回复
热议问题