How does one tell if an IDisposable object reference is disposed?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In .NET, IDisposable does not include an IsDisposed property. That means you generally cannot ask arbitrary disposable objects whether they are disposed unless the type explicitly exposes that state. This is by design: disposal is a behavioral contract (Dispose()), not a universal introspection API.
So how do you "tell" if an object is disposed? In practice, you either track disposal in your own type, use safe usage patterns that avoid needing the check, or handle expected post-disposal exceptions for third-party types.
Core Sections
1. Implement explicit disposal tracking in your own classes
When you control the type, keep a private flag and guard public methods.
This gives deterministic behavior and clear diagnostics.
2. Prefer usage patterns that avoid external disposal checks
For external objects, best practice is to scope lifetime with using and avoid cross-scope references.
When disposal ownership is shared or asynchronous, wrap dependencies with clear ownership semantics (factory, lifetime scope, DI container) instead of asking runtime state repeatedly.
3. For foreign types, handle ObjectDisposedException
If type does not expose state and you cannot control lifecycle perfectly, catch expected disposal exceptions at boundary points.
Do not use exception handling as normal control flow in hot paths, but it is valid for race-prone shutdown paths.
4. Concurrency and thread-safety
Disposal checks can race with disposal in multithreaded code. A check like if (!IsDisposed) Use() is not atomic.
Safer approaches:
- lock around both check and use,
- use cancellation tokens to coordinate shutdown,
- centralize disposal in one owner thread.
Common Pitfalls
- Assuming every
IDisposableobject can report disposal state via a common API. - Exposing
IsDisposedbut forgetting to enforce it in all public methods. - Performing non-atomic check-then-use patterns in concurrent code.
- Catching broad exceptions instead of
ObjectDisposedExceptionfor disposal-related paths. - Sharing disposable dependencies across scopes without clear ownership contracts.
Summary
There is no universal way to query disposal state for arbitrary IDisposable references. For your own types, track and enforce _disposed explicitly. For external types, structure code around ownership scopes and handle ObjectDisposedException where races are expected. Good lifetime design is more reliable than ad hoc disposal-state probing.
A reliable strategy in larger systems is to model disposal ownership explicitly. For example, repositories may own DB connections per request scope, while singletons should never own scoped disposables directly. Dependency injection containers can enforce lifetimes and reduce manual disposal checks spread across business logic. When disposal ownership is clear, the need to query "is disposed" often disappears naturally.
Testing disposal behavior is important as well. Add unit tests that assert methods throw ObjectDisposedException after Dispose() and that multiple dispose calls are safe (idempotent). For async-capable resources, consider IAsyncDisposable patterns and await using. These tests turn disposal semantics into enforceable contracts instead of informal assumptions.
Clear lifetime diagrams in architecture docs can eliminate many disposal-related bugs before code is written.
Related reading
- How does one test async code using MSTest
- How does RegexOptions.Compiled work?
- How does String.Equalsa,b not produce a StackOverflowException?
- How does string.Format handle null values?
- How does Taskint become an int?
- How does Taskint become an int?
- How does the following LINQ statement work?
- How does the ThreadStatic attribute work?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.