Back to blog

C#

Serialization - how to save objects to file

But this option is good only if we want to create brand new object. What if we want to save object data to file and recover it later? We can either use StreamReader or serialization!

In Object Oriented programming languages we are able to create new classes and create instances (objects) of these classes. These objects exists only if we construct them using constructor:

But this option is good only if we want to create brand new object. What if we want to save object data to file and recover it later? We can either use StreamReader or serialization!

According to Microsoft C# documentation: "Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file." It means that every object we create can be stored as a file and reconstructed whenever we want. How to save object to file? This process is easier than you expect. Let's begin with creating small class (Code available on my GitHub): And create create object in main function:

To save object to file, we have to: Mark class as Serializable using Seriablizable (it allows as to store all object of class Dog):

Add libraries to solution:

Add FileStream and BinaryFormatter objects:

Why do we have to use FileStream instead of StreamReader or StreamWriter classes?

FileMode.
Append Opens a file if it already exists and moves to the end ready to add new data (if it doesn’t exist it is created)
Create Creates a new file or overwrites an existing one
CreateNew Creates a new file or throws an IOException if the file already exists
OpenOrCreate Opens an existing file or creates it if it doesn’t already exist
Truncate Opens an existing file and once opened truncates it so its size is zero bytes
FileAccess.
Read Data can be read from the file
Write Data can be written to the file
ReadWrite Data can be read from and written to the file
Properties
CanRead Gets a value indicating whether the FileStream supports reading
CanWrite Gets a value indicating whether a FileStream supports writing
CanSeek Gets a value indicating whether the FileStream supports seeking (moving to a specified position)
Length Gets the length of the FileStream in bytes
Name Gets the name of the FileStream
Position Gets or sets the current position of the FileStream

BinaryFormatter on the other hand is one of the classes which give us ability to change our object to actual saveable data. The object of this class is taking the FileStream object and object which has to be serialized as its parameters.

Let's see how the code looks like after adding these objects:

As you probably noticed, you can use the same using(){} method as you can use with StreamReader and StreamWriter classes. If you don't use them, remember to always use .Close() method to close Stream connection. Please mind, that we can also use serialization with arrays.

These are just few of the results which we can receive using serialization. For more please visit Microsoft Webiste.

That's all for now. If you have any questions don't hesitate to contact me!