gpt4 book ai didi

c# - StringBuilder 与 StringWriter/StringReader

转载 作者:IT王子 更新时间:2023-10-29 04:11:17 27 4
gpt4 key购买 nike

我最近读到在StringWriterStringReader 中用于写入和读取StringBuilder

当我使用 StringBuilder 对象时,它看起来是一个自给自足的类。

我们有各种读写 StringBuilder 的方法,使用StringBuilder.Append()Insert()Replace()Remove() 等...

  1. StringWriterStringReader 有什么可能的用途,而这不能由 StringBuilder 本身完成?
  2. 它们的实际用途是什么?
  3. 他们没有将 Stream 作为输入的可能原因是什么(因为任何其他编写者和读者都将流作为要操作的构造函数参数)但 StringBuilder ?

最佳答案

What is the possible use of StringWriter and StringReader, which cannot be done by StringBuilder itself?

StringReaderStringWriter 分别派生自 TextReaderTextWriter。因此,它们可以作为 TextReaderTextWriter 实例,而 stringStringBuilder 不能,因为它们不派生出这些类型中的任何一种。

What is the practical use of them?

它们允许您调用需要 TextReaderTextWriter 的 API,当您拥有/想要的是 string字符串生成器

What could be the possible reason they are not taking up Stream as the input (Because any other writer and reader are taking the stream as the Constructor parameter to operate on) but the StringBuilder?

因为它们不适用于流;它们在 stringStringBuilder 上工作。它们只是简单的包装器类,为需要不同接口(interface)的 API 调整这些类型。请参阅:adapter pattern .

关于c# - StringBuilder 与 StringWriter/StringReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3392794/

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