Stream.Dispose Method

Definition

Releases all resources used by the Stream object.

Overloads

Dispose()

Releases all resources used by the Stream.

Dispose(Boolean)

Releases the unmanaged resources used by the Stream and optionally releases the managed resources.

Dispose()

Releases all resources used by the Stream.

public:
 virtual void Dispose();
public void Dispose ();
abstract member Dispose : unit -> unit
override this.Dispose : unit -> unit
Public Sub Dispose ()

Implements

Remarks

This method disposes the stream, by writing any changes to the backing store and closing the stream to release resources.

Calling Dispose allows the resources used by the Stream to be reallocated for other purposes. For more information about Dispose, see Cleaning Up Unmanaged Resources.

Notes to Inheritors

Place all cleanup logic for your stream object in Dispose(Boolean). Do not override Close().

Note that because of backward compatibility requirements, this method's implementation differs from the recommended guidance for the Dispose pattern. This method calls Close(), which then calls Dispose(Boolean).

Applies to

Dispose(Boolean)

Releases the unmanaged resources used by the Stream and optionally releases the managed resources.

protected:
 virtual void Dispose(bool disposing);
protected virtual void Dispose (bool disposing);
abstract member Dispose : bool -> unit
override this.Dispose : bool -> unit
Protected Overridable Sub Dispose (disposing As Boolean)

Parameters

disposing
Boolean

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Remarks

You should release all resources by specifying true for disposing. When disposing is true, the stream can also ensure data is flushed to the underlying buffer, and access other finalizable objects. This may not be possible when called from a finalizer due a lack of ordering among finalizers.

If your stream is using an operating system handle to communicate with its source, consider using a subclass of SafeHandle for this purpose.

This method is called by the public Dispose() method and the Finalize() method, if it has been overridden. Dispose() invokes the protected Dispose method with the disposing parameter set to true. Finalize invokes Dispose with disposing set to false.

Notes to Inheritors

In derived classes, do not override the Close() method, instead, put all of the Stream cleanup logic in the Dispose(Boolean) method.

Dispose() can be called multiple times by other objects. When overriding Dispose(Boolean), be careful not to reference objects that have been previously disposed of in an earlier call to Dispose(). For more information about how to implement Dispose(Boolean), see Implementing a Dispose Method.

For more information about Dispose() and Finalize(), see Cleaning Up Unmanaged Resources.

Applies to