Saturday 26 March 2011

Serialization Interview Questions

What is serialization and Deserialization?
Serialization is the process of converting an object into a stream of bytes.
Deserialization is the opposite process i.e. it creats an object from a stream of bytes.

Serialization /Deserialization is mostly used to transport objects (e.g. during remoting), or to persist objects (e.g. to a file or database).

What are the types of Serialization?

There are 4 types of srailization.

1. Binary Serialization: It writes the contents of an object into binary form to a file.
2. SOAP Serialization: It writes the contents of an object into platform-agnostic format.
3. XML Serialization: It writes the contents of an object into a XML file.
4. Custom Serialization: In some cases, the default serialization techniques provided by .NET may not be sufficient in real life.  This is when we require implementing  custom serialization.   It is possible to implement custom serialization in .NET by implementing the ISerializable interface.  This interface allows an object to take control of its own serialization and de-serialization process.

Does the .NET Framework have in-built support for serialization?

There are two separate mechanisms provided by the .NET class library - XmlSerializer and SoapFormatter/BinaryFormatter. Microsoft uses XmlSerializer for Web Services, and uses SoapFormatter/BinaryFormatter for remoting. Both are available for use in your own code.

Why is XmlSerializer so slow?
There is a once-per-process-per-type overhead with XmlSerializer. So the first time you serialize or deserialize an object of a given type in an application, there is a significant delay. This normally doesn't matter, but it may mean, for example, that XmlSerializer is a poor choice for loading configuration settings during startup of a GUI application.

Why do we get errors when we try to serialize a Hashtable?

XmlSerializer will refuse to serialize instances of any class that implements IDictionary,
e.g. Hashtable. SoapFormatter and BinaryFormatter do not have this restriction.

what are the namespaces we use to implement serialization?

using System.Xml.Serialization; -> for xml
using System.Runtime.Serialization.Formatters.Binary; -> for binary
using System.Runtime.Serialization.Formatters.Soap; -> for soap



No comments:

Post a Comment