How to check if object has been disposed in C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, there is no universal built-in way to ask an arbitrary object whether it has already been disposed. Disposal is a behavioral contract, not a language-wide state flag that every object exposes publicly.
That means the right answer depends on your role. If you are implementing the disposable type, track disposal internally and throw ObjectDisposedException when the object is used after disposal. If you are consuming some other type, you usually should not probe for disposal at all.
Why There Is No General IsDisposed Check
IDisposable contains only one member: Dispose(). It does not require an IsDisposed property. Some framework types expose their own status, but many do not, and those that do are not consistent.
Because of that, code like "if disposed then skip" is rarely portable or reliable. The object may be disposed between the check and the next operation anyway. In practice, good disposable types protect themselves by rejecting later use.
Implement Disposal Inside Your Own Type
When you own the class, keep a private flag and check it at the start of every public method that requires the object to still be alive:
This pattern is the standard answer because it keeps the invariant inside the object. Callers do not need an IsDisposed property; they either use the object correctly or get a clear exception.
Using a Disposable Object Safely
From the caller side, the usual tool is a using statement or using var, which limits the lifetime explicitly:
This example intentionally uses the object after disposal to show the expected failure mode. In normal code, you avoid that situation by structuring scope correctly rather than checking status before every call.
When You Do Not Own the Type
If the object comes from a library, there may be no supported status property. In that case, the options are limited:
- manage the lifetime externally so you know whether you disposed it
- catch
ObjectDisposedExceptionif later use is possible - wrap the dependency in your own abstraction that tracks state
For example, if one service disposes a stream and another service might still reference it, the real bug is ownership confusion. The fix is to make ownership explicit, not to poll the stream for hidden state.
Common Pitfalls
One pitfall is exposing a public IsDisposed property and encouraging callers to branch on it. That looks convenient, but it often leads to race conditions and spreads lifecycle knowledge into every caller.
Another mistake is setting the disposal flag too early or too late. Set it only after cleanup is complete enough that later method calls should fail. If cleanup can throw, think carefully about how partially disposed state should behave.
Developers also confuse garbage collection with disposal. The garbage collector reclaims managed memory eventually, but it does not replace deterministic cleanup of files, sockets, database connections, or other external resources.
Summary
- There is no general-purpose API that tells you whether any C# object has been disposed.
- For your own types, track a private
_disposedflag and throwObjectDisposedExceptionfrom public methods after disposal. - For library types, manage lifetime explicitly instead of trying to inspect hidden state.
- Prefer
usingand clear ownership rules over defensive disposal checks at every call site.
Related reading
- How to check if property setter is public
- How to check possibility of deadlock in c code
- How to check programmatically if a type is a struct or a class?
- How to check that Request.QueryString has a specific value or not in ASP.NET?
- How to compare 'μ' and 'µ' in C
- How to compare DateTime in C?
- How to compare DateTime without time via LINQ?
- How to compare different branches in Visual Studio Code

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.