为什么 StringBuilder 类不继承自 Stream?
我只是对此感到好奇。 让我印象深刻的是,StringBuilder 的行为在功能上(如果不是技术上)与 Stream 相同——它是一个可以添加其他数据的数据箱。
再说一遍,只是好奇。
I'm just curious about this. It strikes me that the behavior of a StringBuilder is functionally (if not technically) the same as a Stream -- it's a bin of data to which other data can be added.
Again, just curious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
流是二进制数据的输入和输出。
StringBuilder 是构建文本数据的方法。
除此之外,还有状态问题 - StringBuilder 仅具有当前值,没有“位置”的概念。 它允许您访问和修改其中任何位置的数据。 另一方面,流在逻辑上是一个潜在的无限数据流,中间有一个光标来指示您必须到达的位置。 通常,您只需向前读/写,使用“查找/定位”跳到数据流的特定部分。
尝试想象用 StringBuilder 实现 Stream API...它根本不适合。 您可以排序这样做,但基本上您最终会得到 StringReader 和 StringWriter。
Stream is an input and output of binary data.
StringBuilder is means of building up text data.
Beyond that, there's the issue of state - a StringBuilder just has the current value, with no idea of "position". It allows you to access and mutate data anywhere within it. A stream, on the other hand, is logically a potentially infinite stream of data, with a cursor somewhere in the middle to say where you've got to. You generally just read/write forwards, with Seek/Position to skip to a specific part of the data stream.
Try to imagine implementing the Stream API with StringBuilder... it just doesn't fit. You could sort of do it, but you'd end up with StringReader and StringWriter, basically.
StringBuilder
不仅仅具有 Append 功能。 它还具有对于流来说不自然的插入函数。 如果您想要一个包装StringBuilder
的流,请使用StringWriter
类。StringBuilder
has more than just Append functions. It also has insert functions which is unnatural for a stream. Use theStringWriter
class if you want a stream that wraps aStringBuilder
.流通常指外部输入/输出源(文件、网络)。 StringBuilder没有这样的特性。
A stream normally refers to an external input/output source (file, network). StringBuilder has no such characteristic.
因为它并不是真正的流。 它更像是一个不断增长的缓冲区。
Because it's not really a stream. It's more of a buffer that grows.
虽然两者都可以添加数据,但整体功能是不同的。
流用于从某个源输入或输出数据,而不是用于构建某些东西。 StringBuilder 不需要 Stream 提供的功能(例如缓冲等)来构建资源。
While both can have data added to them, the functionality as a whole is different.
A Stream is for inputting or outputting data from/to some source, not for building something. StringBuilder does not need functionality that Stream provides, like Buffering, etc to build a resource.
另一方面,您会在 System.IO 中找到 StringReader/Writer 类。 StringWriter 例如针对底层StringBuilder 实现TextWriter。
就我个人而言,我从未使用过它,但如果您有一个文本文件写入例程,您可以使其针对 TextWriter 工作。 然后在您的测试中,您不是实例化 StreamWriter,而是实例化 StringWriter,然后您可以通过查看底层 StringBuilder 来检查写入的内容。
现在我头晕了...
On the other hand you will find the classes StringReader/Writer in System.IO. The StringWriter e.g. implements TextWriter against an underlying StringBuilder.
Personally I've never used it but if you have a text file writing routine you could make it work against a TextWriter. Then in your test, instead of instantiating a StreamWriter you instantiate a StringWriter and you could then check what was written by looking at the underlying StringBuilder.
Now I'm dizzy...