C# interview question :- What is Serialization in .NET ?

 Posted by articlesmaint on 3/13/2011 | Category: .NET Framework Interview questions | Views: 11221


Serialization is a process by which we can save the state of the object by converting the object in to stream of bytes.These bytes can then be stored in database, files, memory etc.


Below is a simple code of serializing the object.


MyObject objObject = new MyObject();


objObject.Value = 100;              

// Serialization using SoapFormatter


SoapFormatter formatter = new SoapFormatter();


Stream objFileStream = new FileStream("c:\\MyFile.xml", FileMode.Create, FileAccess.Write, FileShare.None);


formatter.Serialize(objFileStream, objObject);


objFileStream.Close();


 


 Below is simple code which shows how to deserialize an object.


 

//De-Serialization


Stream objNewFileStream = new FileStream("c:\\MyFile.xml", FileMode.Open, FileAccess.Read, FileShare.Read);


MyObject objObject =(MyObject)formatter.Deserialize(objNewFileStream);


objNewFileStream.Close();




Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Laghaterohan on: 3/14/2011
Ok! fine but can u let me know...why would ever the object needs to be serialized?

Login to post response