Yippie-Ki-Yay
Yippie-Ki-Yay

Reputation: 22794

C# - stream question

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).

Upvotes: 5

Views: 1011

Answers (4)

Kent Boogaart
Kent Boogaart

Reputation: 178630

Use System.IO.Stream if you only care about bytes. TextReader / TextWriter are for when you know the underlying data to be text.

Upvotes: 3

Felice Pollano
Felice Pollano

Reputation: 33242

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.

Upvotes: 0

George Johnston
George Johnston

Reputation: 32258

The base class is Stream. MemoryStream, FileStream, etc. inherit from this class.

Upvotes: 1

SLaks
SLaks

Reputation: 887225

If you want to work with strings, you should take a TextReader or TextWriter.

If you want to work with bytes, you should take a Stream.

These classes are inherited by concrete implementations such as FileStream, StringWriter, and NetworkStream.

Upvotes: 5

Related Questions