C# - 流问题
当我用 C++
编写一些 I/O 例程时,我通常会通过在
的接口上进行操作,使其尽可能通用。
例如:
void someRoutine(std::istream& stream) { ... }
如何在 C#
中完成同样的操作?
我怀疑我可以基于 System 编写例程.IO.TextReader
或 System.IO.TextWriter
,但我不确定。
显然我正在寻找 C#,与
std::istream
或 std::ostream
一样通用,并且可以通过多种方式进行扩展(例如,boost::iostreams
扩展了 std::
流)。
When I was writing some I/O routine in C++
, I would usually make it as generic as possible, by operating on the interfaces from <iostream>
.
For example:
void someRoutine(std::istream& stream) { ... }
How should the same be done in C#
?
I suspect I could write my routines based on the System.IO.TextReader
or System.IO.TextWriter
, but I'm not sure.
Obviously I'm seeking for a same base class in C#
, which is as generic as std::istream
or std::ostream
and which can be extended in many ways (for example, as boost::iostreams
extends the std::
streams).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想使用字符串,您应该使用
TextReader
或TextWriter
。如果您想使用字节,则应该采用
Stream
。这些类由具体实现继承,例如
FileStream
、StringWriter
和NetworkStream
。If you want to work with strings, you should take a
TextReader
orTextWriter
.If you want to work with bytes, you should take a
Stream
.These classes are inherited by concrete implementations such as
FileStream
,StringWriter
, andNetworkStream
.如果您只关心,请使用 System.IO.Stream字节。
TextReader
/TextWriter
适用于当您知道底层数据是文本时。Use System.IO.Stream if you only care about bytes.
TextReader
/TextWriter
are for when you know the underlying data to be text.基类是
Stream
。 MemoryStream、FileStream等继承自该类。The base class is
Stream
. MemoryStream, FileStream, etc. inherit from this class.您可以让 C# 函数采用 Stream ( System.IO.Stream ),就像在 C++ 中一样。这是否合适取决于您编写的函数。
You can have the C# function taking a Stream ( System.IO.Stream ) as in C++. If this is appropriate depends on the function you write.