serialization in
serialization in
Deserialization in

Serialization In C#

Serialization In C#


Here, Byte streams are a sequence of bytes used by programs to input and output information.

Byte Stream

Real Life Example

Real Life Example

Serialization in

  • In C#, Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file.
  • In C#, Its main purpose is to save the state of an object in order to be able to recreate it when needed.
  • In C#, the reverse process is called “De-Serialization”.

The general steps for serializing are:

  • Create an instance of File that will store serialized object.
  • Create a stream from the file object.
  • Create an instance of BinaryFormatter.
  • Call serialize method of the instance passing it stream and object to serialize.

How serialization works

  • In C#, the object is serialized to a stream that carries the data.
  • In C#, the stream may also have information about the object’s type, such as its version, culture, and assembly name.
  • From that stream, the object can be stored in a database, a file, or memory.

Uses for serialization

  • In C#, Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange.
  • In C#, Through serialization, a developer can perform actions such as:
    • Sending the object to a remote application by using a web service
    • Passing an object from one domain to another
    • Passing an object through a firewall as a JSON or XML string
    • Maintaining security or user-specific information across applications

Important Points

  • In C#, If you want to make a class Serializable then you have to use [Serializable] attribute on top of your class.
  • If you have applied [Serializable] attribute to the class then that class will not be inherited.
  • Namespace used for this purpose System.Runtime.Serialization.Formatters.Binary
  • We can serialize data in XML and JSON as well.


  1. Serializing In Binary
  2. Serializing In XML
  3. Serializing In JSON

De-serialization in

serialization in
Deserialization in

What is Deserialization in C# ?

  • In C#, As the name suggests, deserialization in C# is the reverse process of serialization.
  • In C#, deserialization is the opposing process which takes data from a file, stream or network and rebuilds it into an object.
  • In C#, It resurrects the state of the object by setting properties, fields etc.

Source Code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Serialization_Demo
    class Program
        static void Main(string[] args)
            // SERIALIZATION

            //string path = @"G:\Adil New\Sample.exe";
            //Employee emp = new Employee(241, "Adil");
            //FileStream stream = new FileStream(path, FileMode.OpenOrCreate);
            //BinaryFormatter bf = new BinaryFormatter();
            //bf.Serialize(stream, emp);
            //Console.WriteLine("File Created Successfully -> " + path);

            // DE-SERIALIZATION
            string path = @"G:\Adil New\Sample.pdf";
            FileStream stream = new FileStream(path, FileMode.OpenOrCreate);
            BinaryFormatter bf = new BinaryFormatter();
            Employee emp = (Employee)bf.Deserialize(stream);
            Console.WriteLine("Employee Id: " + emp.Id);
            Console.WriteLine("Employee Name: " + emp.Name);


Download Notes & Source Code Of This Blog, From The Link Given Below.

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *