Stream.Dispose Stream.Dispose Stream.Dispose Stream.Dispose Method

定義

釋放由 Stream 物件使用的所有資源。Releases all resources used by the Stream object.

多載

Dispose() Dispose() Dispose() Dispose()

釋放 Stream 所使用的所有資源。Releases all resources used by the Stream.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

釋放 Stream 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the Stream and optionally releases the managed resources.

Dispose() Dispose() Dispose() Dispose()

釋放 Stream 所使用的所有資源。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 ()

實作

備註

這個方法會處置資料流,寫入備份存放區中的任何變更並關閉資料流釋放資源。This method disposes the stream, by writing any changes to the backing store and closing the stream to release resources.

呼叫Dispose可讓所使用的資源Stream重新配置用於其他用途。Calling Dispose allows the resources used by the Stream to be reallocated for other purposes. 如需詳細資訊Dispose,請參閱 < 清除 Unmanaged 資源總For more information about Dispose, see Cleaning Up Unmanaged Resources.

給繼承者的注意事項

將您的資料流物件中的所有清除邏輯Dispose(Boolean)Place all cleanup logic for your stream object in Dispose(Boolean). 不會覆寫Close()Do not override Close().

請注意,基於回溯相容性需求,此方法的實作不同的建議的指導方針的處置模式。Note that because of backward compatibility requirements, this method's implementation differs from the recommended guidance for the Dispose pattern. 這個方法會呼叫Close(),接著呼叫Dispose(Boolean)This method calls Close(), which then calls Dispose(Boolean).

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

釋放 Stream 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。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)

參數

disposing
Boolean Boolean Boolean Boolean

true 表示會同時釋放 Managed 和 Unmanaged 資源,false 則表示只釋放 Unmanaged 資源。true to release both managed and unmanaged resources; false to release only unmanaged resources.

備註

您應該釋放所有資源,藉由指定true針對disposingYou should release all resources by specifying true for disposing. disposingtrue,資料會排清基礎緩衝區中,並存取其他可完成的物件,也可以確保資料流。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.

如果您的資料流使用的作業系統控制代碼和它的來源進行通訊,請考慮使用的子類別SafeHandle針對此目的。If your stream is using an operating system handle to communicate with its source, consider using a subclass of SafeHandle for this purpose.

這個方法會呼叫公用Dispose方法和Finalize方法。This method is called by the public Dispose method and the Finalize method. Dispose 叫用受保護Dispose方法disposing參數設為trueDispose invokes the protected Dispose method with the disposing parameter set to true. Finalize 叫用Dispose具有disposing設定為falseFinalize invokes Dispose with disposing set to false.

給繼承者的注意事項

在衍生類別中,不會覆寫Close()方法,而是將所有中的 Stream 清除邏輯Dispose(Boolean)方法。In derived classes, do not override the Close() method, instead, put all of the Stream cleanup logic in the Dispose(Boolean) method.

Dispose() 可以被其他物件呼叫多次。Dispose() can be called multiple times by other objects. 覆寫 Dispose(Boolean) 時,請小心不要參考到先前已在對 Dispose() 的早期呼叫中被處置 (Dispose) 的物件。When overriding Dispose(Boolean), be careful not to reference objects that have been previously disposed of in an earlier call to Dispose(). 如需有關如何實作Dispose(Boolean),請參閱 <<c2> 實作 Dispose 方法For more information about how to implement Dispose(Boolean), see Implementing a Dispose Method.

如需詳細資訊Dispose()Finalize(),請參閱清除 Unmanaged 資源總For more information about Dispose() and Finalize(), see Cleaning Up Unmanaged Resources.

適用於