gpt4 book ai didi

c# - 你必须刷新你的 BinaryWriter 数据吗

转载 作者:行者123 更新时间:2023-12-05 02:17:23 24 4
gpt4 key购买 nike

在 C# 中,我像这样使用 BinaryWriter:

BinaryWriter br = new BinaryWriter(myPipe);
var bytes = new byte[5];
br.Write(bytes);
br.Flush();

https://msdn.microsoft.com/de-de/library/system.io.binarywriter.flush(v=vs.110).aspx提到你应该使用刷新你的二进制编写器。

但是我注意到一旦调用 br.Write 就已经发送了数据,我认为不需要调用 br.Flush?

我想知道是否可以删除那 1 行(不必要的)代码:br.Flush() 或者是否还有更多代码?

最佳答案

这完全取决于所使用的流。刷新流意味着任何尚未写入的缓冲数据将在刷新期间写入。因此,如果您使用的流不缓冲任何数据,您将不会看到任何差异。如果您不刷新流,它将在您关闭它时被刷新。

您提到的文档仅说明:

All derived classes should override Flush to ensure that all buffered data is sent to the stream.

Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close. Setting AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

关于c# - 你必须刷新你的 BinaryWriter 数据吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48051566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com