Serialization is the technique used by the .NET runtime. The runtime serialization functionality resides in the System.Runtime.Serialization namespace. It involves converting all of the object's data into a byte stream. It allows you to treat all of the object's data as a single value. The entire state of the object's data is represented by a series of bytes.

Your application can write the serialized data to a target stream. For instance, you can persist objects in some kind of storage. It can be a disk file or a database. It can even be written to a MemoryStream.

The serialization stream includes the following:

  1. the serialized data
  2. the object's type information that describes the data: It includes its assembly name, culture, and version. The type metadata is also included. Deserialization process uses this information to recreate the object.


The application can then deserialize the stream for later use. Deserialization converts the byte stream back into individual data members. The object is recreated from its serialized representation.

Uses of Serialization

Serialization is typically used in the following scenarios:

  1. You can recreate the same object whenever required. Save the object's state and then continue from that place afterward.

  2. You can transfer the object's data across the network.

  3. You may want to reset all the object's data back to some persisted state i.e. undo the object's state.

  4. It can also be used for creating the clone of the object.

Shallow and Deep Serialization

.NET framework offers two basic types of serialization.

a. Shallow serialization: Only the public fields and read-write properties of the object are serialized into a byte stream.

b. Deep serialization: The entire state of the object is serialized including the values of its private fields. The entire object graph is serialized as well i.e. if the object holds a reference to other objects, even those are serialized. For e.g. Order containing a reference to Customer and various Items.

IFormatter interface and its implementations

The IFormatter interface resides in the System.Runtime.Serialization namespace. It provides the Serialize() and Deserialize() methods that encapsulate the serialization and deserialization functionality respectively.

A formatter specifies the serialization format for the objects. All formatters implement the IFormatter interface. The .NET framework provides certain built-in formatters. For example, SoapFormatter and BinaryFormatter are the concrete formatter classes which can be used to serialize and derserialize the objects. They differ in the way they format the byte stream.

Binary Serialization

To achieve binary serialization, you need to use the BinaryFormatter which resides in the System.Runtime.Serialization.Formatters.Binary namespace. It uses the deep serialization technique and generates a data stream in binary format. It is useful either for storage or for sending data across the network. The stream produced is more compact when compared to the other methods.

Soap Serialization

To achieve SOAP serialization, you need to use the SoapFormatter which resides in the System.Runtime.Serialization.Formatters.Soap namespace. It also uses the deep serialization technique. It generates a data stream in XML format based on the SOAP-standard i.e. SOAP-protocol specifics are also incorporated during the serialization process.

XML is designed to exchange data across heterogeneous systems. SOAP messages piggyback on HTTP and are able to penetrate Internet firewalls. Therefore this serialization format comes handy when you need to send the data through firewalls or heterogeneous systems. Note that BinaryFormatter is not appropriate in such situations.

For Part 2, click this link: Serialization in .NET - Part 2