Sunday, 23 June 2013

Serialization in C#.net with binary serialization

Serialization is the process of converting objects in to persistent data formats.
.NET 2.0 provides built-in classes to convert data to formats that are portable, 
or easy to transport to another location. 
This process of converting data into a portable format is called serialization. 
The process of restoring the object or data to its original state is called deserialization.

Portable/persistent format includes text files or series of bytes or we can store in the database.
The process of converting objects and data into these common format for storage 
or transportation is called serialization.

The following are the basic advantages of serialization:
1.Serialization Creates a clone of an object
2.It facilitate the transportation of an object through a network

We can achieve Serialization by any  of the following types:
 1.Binary Serialization
 2.SOAP Serialization
 3.XML Serialization

In this post we are going to discuss about Binary Serialization,

Binary Serialization:
To determine the serialization format for objects, you need to use a formatter.
A formatter is required to indicate how you want to format the data that you are going 
to save for deserialization.

This Serialization in implemented by System.Runtime.Serialization.Formatters.Binary.BinaryFormatter class.

BinaryFormatter serializes and deserializes an object, or an entire graph of connected objects, in binary format.

The most common format in which objects and data can be serialized when storing or passing them between threads 
or application domains is the binary format. 

These application domains may reside on the same computer or in a controlled environment such as a network. 

The binary format is a series of bytes that represent the original state of the serialized object or data. 

At times, you may need to transfer data between applications that reside in completely different environments. 

In this situation, you can pass serialized objects and data between applications by using the Web. 

When passing an object or data over the Internet, the object must be serialized to a suitable format.

BinaryFormatter class generates a compact stream when you serialize an object. So it is useful for storage 
and socket-based network streams. 

Serializes all the members of an object, such as properties and enumerations, including the private members.

This allows BinaryFormatter to store all state data.
The following code Snippet shows how we can implement Binary Serialization.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Demo
    //create a class whose object can be serialized
    [Serializable]      //then mark the type as Serializable by using Serializable attribute
    class Product
        private int prodid;
        private string prodname;

        public Product(int prodid, string prodname)
            this.prodid = prodid;
            this.prodname = prodname;
        public void Show()
            Console.WriteLine("Product id {0}", prodid);
            Console.WriteLine("Product name {0}", prodname);

    class BinarySerializationDemo
        static void Main()
            Product p = new Product(1001, "Computer");

            //Create a object of FileStream to save the serialized data
            FileStream fs = new FileStream("..\\..\\productsData.txt", FileMode.Create, FileAccess.Write);

            //create a BinaryFormatter
            BinaryFormatter fmt = new BinaryFormatter();

            //serialize the object by using Serialize() methode
            fmt.Serialize(fs, p);
            Console.WriteLine("Product object serialized");

            //deserialize the object
            fs = new FileStream("..\\..\\productsData.txt", FileMode.Open, FileAccess.Read);
            p = (Product)fmt.Deserialize(fs);
            Console.WriteLine("Product object deserialized");
Hope this post will help you for simple implementation of binary serialization.
Happy Coding !!! Have some look on .NET Quizzes