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 占用的非托管资源,还可以另外再释放托管资源。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,请参阅清理了非托管资源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 占用的非托管资源,还可以另外再释放托管资源。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 表示释放托管资源和非托管资源;false 表示仅释放非托管资源。true to release both managed and unmanaged resources; false to release only unmanaged resources.

注解

你应通过指定释放所有资源truedisposingYou 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 调用Disposedisposing设置为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() 时已释放的对象。When overriding Dispose(Boolean), be careful not to reference objects that have been previously disposed of in an earlier call to Dispose(). 有关如何实现详细信息Dispose(Boolean),请参阅实现 Dispose 方法For more information about how to implement Dispose(Boolean), see Implementing a Dispose Method.

有关详细信息Dispose()Finalize(),请参阅清理了非托管资源For more information about Dispose() and Finalize(), see Cleaning Up Unmanaged Resources.

适用于