Martin Bliss
Martin Bliss

Reputation: 1051

C# Arrays vs. Streams

When writing functions that operate on a potentially infinite "stream" of data, i.e. bytes, chars, whatever, what are some design considerations in deciding to use Strings/Arrays vs. Streams for the input/output?

Is there a huge performance impact of always writing functions to use streams, and then making overload methods that use stream wrappers (i.e. StringReader/Writer) to return "simple data" like an array or a string that doesnt require disposing and other considerations?

I would think functions operating on arrays are far more convenient because you can "return" a resulting array and you don't usually have to worry about disposing. I assume stream operators are nice because they can operate on an infinite source of data, probably memory-efficient as well.

Upvotes: 3

Views: 1790

Answers (2)

cdiggins
cdiggins

Reputation: 18203

If you are writing a function to process a stream of data, then why not pass it as an IEnumerable<T>. You can then return a stream as an IEnumerable<T> in a generator function. In other words using return yield to return each result one a ta time.

You can end up with asymptotic improvements in performance in some cases because the evaluation is done as needed.

Upvotes: 1

Magnus
Magnus

Reputation: 46919

If you are working with binary data of unknown size always use streams. Reading an entire file into a byte array for example is usually bad idea if it can be avoided. Most functions in .Net that work with binary data such as encryption and compression are built to use streams as input/output.

Upvotes: 1

Related Questions