Memory Streams

To emphasize I/O is much more than just files, let’s start by using streams to look at in-memory data. The following code uses a byte array as an in-memory storage for data that gets surfaced through two memory streams. Each stream has an offset in the array and can be marked as read-only. In some sense, a stream acts as a “view” of the underlying raw data.

image

Figure 28.5 shows how things are laid out in memory. Each stream has a position that is automatically updated by operations, such as ReadByte. Because of this, you can see subsequent calls to ms2 print values from consecutive cells of array memory. Various Seek operations are ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.