C#
object disposal
garbage collection
programming
.NET

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.

Browse interview questions

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:

csharp
1using System;
2using System.IO;
3
4public sealed class ReportWriter : IDisposable
5{
6    private readonly StreamWriter _writer;
7    private bool _disposed;
8
9    public ReportWriter(string path)
10    {
11        _writer = new StreamWriter(path);
12    }
13
14    public void WriteLine(string value)
15    {
16        ThrowIfDisposed();
17        _writer.WriteLine(value);
18    }
19
20    public void Dispose()
21    {
22        if (_disposed)
23        {
24            return;
25        }
26
27        _writer.Dispose();
28        _disposed = true;
29    }
30
31    private void ThrowIfDisposed()
32    {
33        if (_disposed)
34        {
35            throw new ObjectDisposedException(nameof(ReportWriter));
36        }
37    }
38}

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:

csharp
1using var writer = new ReportWriter("report.txt");
2writer.WriteLine("first line");
3
4writer.Dispose();
5
6try
7{
8    writer.WriteLine("second line");
9}
10catch (ObjectDisposedException ex)
11{
12    Console.WriteLine(ex.ObjectName);
13}

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 ObjectDisposedException if 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 _disposed flag and throw ObjectDisposedException from public methods after disposal.
  • For library types, manage lifetime explicitly instead of trying to inspect hidden state.
  • Prefer using and clear ownership rules over defensive disposal checks at every call site.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.