System.ObjectDisposedException Class

The exception that is thrown when an operation is performed on a disposed object.

See Also: ObjectDisposedException Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public class ObjectDisposedException : InvalidOperationException

Remarks

Note: For additional information about disposing objects, see the IDisposable interface.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Example

The following example demonstrates an error that causes the ObjectDisposedException exception to be thrown.

C# Example

using System;
using System.IO;

public class ObjectDisposedExceptionTest {
   public static void Main() {     
      MemoryStream ms = new MemoryStream(16);
      ms.Close();
      try {
         ms.ReadByte();
      } 
      catch (ObjectDisposedException e) {
         Console.WriteLine("Caught: {0}", e.Message);
      }
   }
}

The output is

Caught: Cannot access a closed Stream.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0