Serialization and Deserialization in C# with Example

Serialization & Deserialization in C#

The concept of Serialization and deserialization is used whenever data pertaining to objects have to be sent from one application to another. Serialization is used to export application data into a file. The destination application then uses deserialization to extract the data from the application for further use.

Serialization is a concept in which C# class objects are written or serialized to files. Let’ say you had a C# class called Tutorial. And the class has 2 properties of ID and Tutorials name.

Serializing can be used to directly write the data properties of the Tutorial class to a file. Deserialization is used to read the data from the file and construct the Tutorial object again.

How to Serialize an Object in C#

Let’s look at an example of how we can achieve this.

In our example, we are going to perform the below high-level steps in the code

  1. Create a class called Tutorial which has 2 properties, namely ID, and Name
  2. We will then create an object from the class and assign a value of “1” to the ID property and a value of “.Net” to the name property.
  3. We will then use serialization to serialize the above object to a file called Example.txt
  4. Finally, we will use deserialization to deserialize the object from the file and display the values in the Console.

Enter the below code in the program.cs file of the console application.

Step 1) Add the class.

The first step is to add the class which will be used for serialization

Serialize an Object in C#

Code Explanation:-

  1. The class which needs to be serialized needs to have the [Serializable] attribute. This is a keyword in C#. This keyword is then attached to the Tutorial class. If you don’t mention this attribute, you will get an error when you try to serialize the class.
  2. Next is the definition of the class which will be serialized. Here we are defining a class called “Tutorial” and providing 2 properties, one is “ID” and the other is “Name.”

Step 2) Create the object.

In this step, first we will create the object of the Tutorial class and serialize it to the file called Example.txt

Serialize an Object in C#

Code Explanation:-

  1. First, we create an object of the Tutorial class. We then assign the value of “1” to ID and “.net” to the name property.
  2. We then use the formatter class which is used to serialize or convert the object to a binary format. The data in the file in serialization is done in binary format. Next, we create a file stream object. The file stream object is used to open the file Example.txt for writing purposes. The keywords FileMode.Create and FileMode.Write is used to specifically mention that the file should be opened for writing purposes.
  3. Finally, we use the Serialize method to transfer the binary data to the file. We then close the stream, since the write operation is complete.

Step 3) Use deserialization.

Finally to ensure that the data is present in the file, we use deserialization to deserialize the object from the file.

Serialize an Object in C#

using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
  class Tutorial
  public int ID;
  public String Name;
   static void Main(string[] args)
    Tutorial obj = new Tutorial();
    obj.ID = 1;
    obj.Name = ".Net";

    IFormatter formatter = new BinaryFormatter();
    Stream stream = new FileStream(@"E:\ExampleNew.txt",FileMode.Create,FileAccess.Write);

    formatter.Serialize(stream, obj);

    stream = new FileStream(@"E:\ExampleNew.txt",FileMode.Open,FileAccess.Read);
    Tutorial objnew = (Tutorial)formatter.Deserialize(stream);



Code Explanation:-

  1. We create the object “stream” to open the file Example.txt in reading only mode.
  2. We then use the formatter class which is used to deserialize the object, which is stored in the Example.txt file. The object returned is set to the object objnew.
  3. Finally, we display the properties of the object “objnew” to the console using the “ID” and “name” properties.

When the above code is set, and the project is run using Visual Studio, you will get the below output.


Serialize an Object in C#

You can see from the above output that the values from the file were deserialized properly and displayed in the console.


Serialization is used to write class objects to files.

De-Serialization is used to recover the objects from the file.