It is recommended to do so whenever a class implements the interface IDisposable
. Unfortunately the compiler and even Visual Studio do not help to guarantee this. I even understand that the compiler does not oblige because there are rare legitimate cases that should not use this form.
But an additional configurable tool should help. Luckily there is Resharper that may alert you that whenever the class you are instantiating is not in a using
. It’s the only way I know but others plugins its competitors must have something similar. Obviously the alert is only made for classes that implement the IDisposable
.
It is also possible to customize the new .NET Compiler Platform to do this. I don’t have the data to tell you how to do this and it shouldn’t be too trivial, although it’s also not very complex. The idea of the new compiler platform is precisely to let people extend its operation according to their needs.
The question was not very clear and I edited based on your comments, see if it got better.
– Maniero