Quick way to determine if a class instance is IDisposable?

I frequently find myself tracing through the class hierarchy to determine if a class is IDisposable. Or even worse, examining a class to determine if it should be IDisposable.

Sometimes they are; sometimes they aren't.

Is there an easier faster way?
deleydAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
just try to call the .Dispose() method. If it compiles, your instance supports the IDisposable interface.

you can also try to create an instance with the "using" syntax. for example string is not disposable so it won't compile:
            using (string s = "abc")
            {

            }

Open in new window

0
it_saigeDeveloperCommented:
If you are talking about a logic check to determine if a type implements IDisposable, simply imply that your object *is* IDisposable or cast *as* IDisposable and check for null; e.g. -
using System;

namespace EE_Q29084893
{
    class Program
    {
        static void Main(string[] args)
        {
            var items = new object[] { new NonDisposableClass(), new DisposableClass(), new NonDisposableStruct(), new DisposableStruct(), "", 6 };
            foreach (var item in items)
            {
                if (item is IDisposable || (item as IDisposable) != null)
                {
                    Console.WriteLine($"{item.GetType()} is Disposable");
                }
                else
                {
                    Console.WriteLine($"{item.GetType()} is Not Disposable");
                }
            }
            Console.ReadLine();
        }
    }

    class NonDisposableClass
    {

    }

    class DisposableClass : IDisposable
    {
        private bool disposedValue = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects).
                }

                disposedValue = true;
            }
        }

        ~DisposableClass()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
        }
    }

    struct NonDisposableStruct
    {

    }

    struct DisposableStruct : IDisposable
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}

Open in new window

Produces the following output -Capture.PNG

-saige-
0
deleydAuthor Commented:
Thank you everyone. I'm actually looking for a fast way when reviewing code to determine if something is IDisposable and we forgot to dispose of it.
0
it_saigeDeveloperCommented:
Other than using the object browser, tracing through the code as you already are or performing a keyword search; I know of no other sensible way.

-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.