Chapter 18 Streams

What’s in This Chapter

  • Stream, FileStream, and MemoryStream classes
  • Stream readers and writers
  • Opening text files
  • Special stream classes

Wrox.com Downloads for This Chapter

Please note that all the code examples for this chapter are available as a part of this chapter’s code download on the book’s website at www.wrox.com/go/csharp5programmersref on the Download Code tab.

At a basic level, all pieces of data are just collections of bytes. The computer doesn’t actually store invoices, employee records, and recipes. At its most basic level, the computer stores bytes of information. (Or even bits, but the computer naturally groups them in bytes.) It is only when a program interprets those bytes that they acquire a higher-level meaning that is valuable to the user.

Usually it’s not helpful to treat high-level data as undifferentiated bytes, but there are times when it’s useful to ignore the higher-level structure of the data and treat it as just a bunch of bytes.

One important way of thinking about data is the stream, an ordered series of bytes. Files, data flowing across a network, messages moving through a queue, and even the memory in an array can all fit this description.

Defining the abstract idea of a stream lets applications handle these different types of objects uniformly. For example, a cryptographic algorithm can process the bytes in a stream without knowing whether they represent employees, prescription information, or an image.

Visual Studio provides ...

Get C# 5.0 Programmer's Reference 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.