Search code examples
c#.netvisual-studiointellisenseidisposable

Recognize Disposable Objects in Visual Studio?


It is suggested that IDisposable objects should be disposed in either using statement or by calling Dispose() method. I find it is not intuitive to find out if an object is disposable in Visual Studio.

My question is: is there any way to recognize IDisposable objects in VS?


Solution

  • If you want to highlight disposable objects differently in VS please check out this post. I personally prefer Resharper answer as I always use R#.

    If you just want to figure out if your object is an instance of some interface you can right-button-click on the variable name and Navigate -> Object Browser or Go to Declaration and then right-button-click on class name Go to Definition/Peek Definition.

    enter image description here

    You might like Peek Definition as it shows everything you need inline:

    enter image description here

    You can always check what methods object has, if it has Dispose() method then 99.9% it's a disposable object. I'll give this 0.01% for those who give methods bad names :).