If my class (which in this case, takes care of database connections) implements IDisposable, is there a way I can force the use of of the using statement when instantiating my class? Or perhaps produce a compiler warning if this wasn't done?
eg:
MyDAL = new MyDal(); // Warns or errors
using (MyDAL = new MyDAL()) // Does not warn or error
No, there's no language level support for this.
However, you may find that tools such as FxCop or ReSharper can warn about it. (You'd need to work out how to integrate that into your workflow.)
Not at compile time, but you can make the finalizer throw an exception in debug mode.
That should tell you when the object wasn't disposed properly during debugging.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With