Implementing IDisposable

The IDisposable is probably one of the most abused interfaces in .Net. Except from all the cases where you actually have an unmanaged resource you need to release I’ve seen it being used a lot of times (including by myself) just to guarantee some code is executed immediatly when a variable go out of scope. I’ve used this in test utilities to make sure certain validation happens before the test execution ends but I’ve also seen this used in non-test code. The people who like to use IDisposable probably (like myself) have some experience from C++ and using smart pointers or similar there. I wish there was a similar construction in .Net where I could force execution of a certain method on an object immediatly when it goes out of scope if (and only if) I explicitly want that. Kind of an IDisposable that does not generate FxCop warnings when not disposed properly…

Anyway, the best guid I’ve seen so far on how to actually implement IDisposable can be found here. Follow those guidelines and do what I do; stop abuse IDisposable constructs just because you think they make the code look neat.

Comments (1)

  1. Jason Haley says:

    Interesting Finds: February 8, 2013