Streams and Serialisation

Streams of data.

What is a data Stream?

  • Like a flow of water, a data stream is a flow of data.
    • Streams can come from any origin: Lake, Fountain or Mountain.
    • Data Streams can come from anywhere too: File, network or web server.
  • What matters is that we have the data we can deal with.
    • This is an abstraction, removing detail of the origin.

What is the data?

  • All data is an array of bytes
    • When streaming from YouTube or Netflix, the server is sending bytes to your computer.
  • The bytes are then converted to something we can use such as a string of text or a video.

Activity - Streams on YouTube

Let's watch a music video on YouTube and watch the streams come in.

[Let's get started](Activity1/Streams with YouTube.html)

Types of streams

File stream


  • Read or Write data from a file
  • Constructor takes 3 parameters:
    1. The path of the file to open.
    2. The FileMode, how the file should be opened.
    3. The FileAccess, what we are allowed to do with this stream.
FileStream fileStream = new FileStream(

Memory stream


  • A stream that exists in memory.
    • It does not need an origins, it just exists.
  • Software Architecture may require streams to be passed between objects.
    • E.g. A Reader Service and a Generator.

Stream helpers

  • Streams are not always asy for developers to use.

    • There are some helper objects that make life easier. 😌
  • StreamWriter - Helps develops more easily write to streams.

  • StreamReader - Helps develops more easily read from streams.

Stream positions

  • When we first create the stream, position is 0.
  • As we read/write the position moves on.
    • Developers can also write code change the position of the stream.
    • This is important when resetting the stream back to 0.

Open and close

  • Streams need to be opened and closed when we use them.
Stream stream;
// Do some work.
  • It is easier for us to use the using statement.
    • This will automatically close a stream for us.
using (Stream stream)
  // Do some work.


  • Representing an object as text so that it can be stored.
    • Stored serialised objects is a database or on the file system.
  • Save objects as special formats such as XML and JSON.

Serialision attribute

In C#, classes need to be decorated with the Serializable attribute.

  • This tells to compiler that this class can be serialised.


  • Objects need to be formatted in a certain way. One formatter is the BinaryFormatter.
    • This formats the object as binary.


  • JSON is a popular format for storing data.
  • We can use a library called JSON.NET
string json = JsonConvert.SerializeObject(new {
  Name = "Apple,
  Expiry = new DateTime(2008, 12, 28),
  Sizes = new string[] { "Small" }
// {
//   "Name": "Apple",
//   "Expiry": "2008-12-28T00:00:00",
//   "Sizes": [
//     "Small"
//   ]
// }

Activity - Stream Unit Tests

We're going to create some Unit Tests to test reading and writing streams.

[Let's get started](Activity2/Stream Unit Tests.html)

That's all folks!

What have we learnt?

  • Streams are arrays of byte data.
  • We can read and write to files using streams.
    • As streams are read there position changes.
  • There a different types of stream.
  • Serialisation is the process of retrospecting data as text.
    • Objects can be serialised using special formatters.
    • Objects can also be serialised as JSON or another format.
© Luke O'Brien 2018