BCL mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe. System.InvalidOperationException

Represents the error that occurs when an operation is performed on a disposed object.

For additional information about disposing objects, see the interface.

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

`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.

- Constructor
  A containing the name of the disposed object.

  Constructs and initializes a new instance of the class.

  This constructor initializes the property of the new instance using The property is initialized to a system-supplied message that describes the errorand includes . This message takes into account the current system culture.

  The property of the new instance is initialized to .

  If is , the property contains only an error message.

  The following example displays the error message of a instance created using this constructor.

  using System;

  public class ExampleDisposableObject : IDisposable { public static void Main() {

  ExampleDisposableObject obj = new ExampleDisposableObject();

  obj.Close();

  try { Console.WriteLine(obj); } catch (ObjectDisposedException e) { Console.WriteLine("Caught: {0}", e.Message); } }

  public ExampleDisposableObject() { isDisposed = false; }

  ~ExampleDisposableObject() { Dispose(true); }

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

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

  public void Dispose(bool disposing) { isDisposed = true; }

  public override String ToString() { if(isDisposed) throw new ObjectDisposedException("ExampleDisposableObject"); else return "This is an instance of ExampleDisposableObject."; }

  private bool isDisposed; }

  The output is

  Caught: Cannot access a disposed object named "ExampleDisposableObject".

  Object name: "ExampleDisposableObject".

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  2.0.0.0
  4.0.0.0
- Constructor
  A containing the name of the disposed object.
  A that describes the error. The content of is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
  To be added.

  Constructs and initializes a new instance of the class.

  This constructor initializes the property of the new instance using , and the property using . If is , the property is initialized to the system-supplied message provided by the constructor that takes no arguments.

  The property of the new instance is initialized to .

  The following example throws a instance created using this constructor.

  using System;

  public class ExampleDisposableObject : IDisposable { public static void Main() {

  ExampleDisposableObject obj = new ExampleDisposableObject();

  obj.Close();

  try { Console.WriteLine(obj); } catch (ObjectDisposedException e) { Console.WriteLine("Caught: {0}", e.Message); } }

  public ExampleDisposableObject() { isDisposed = false; }

  ~ExampleDisposableObject() { Dispose(true); }

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

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

  public void Dispose(bool disposing) { isDisposed = true; }

  public override String ToString() { if(isDisposed) { string message = "Oh-oh! This object has been disposed!"; string objectName = "ExampleDisposableObject"; throw new ObjectDisposedException(objectName, message); } else return "Hello, World!"; }

  private bool isDisposed; }

  The output is

  Caught: Oh-oh! This object has been disposed!

  Object name: "ExampleDisposableObject".

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Void `` `` To be added. To be added. To be added. To be added. 1.0.5000.0 2.0.0.0 4.0.0.0
- Property
  System.String

  Gets the message that describes the error.

  A that describes the error.

  If the property is not , the message includes the name of the object.

  This property is read-only.

  This property overrides .

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Property
  System.String

  Gets the name of the disposed object.

  A containing the name of the disposed object.

  If this property is not or , the value of this property is included in the string returned by the property.

  This property is read-only.

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
0 System.Runtime.InteropServices.ComVisible(true)