.Net Interview Questions and Answers

Search:

Serialization and Deserialization in C# step by step

By : Shiv Prasad Koirala | Dec 9th, 2016 | Views : 1363 | Comments : 0 Bookmark and Share

Serialization


Serialization is converting object to stream of bytes. It is a process of bringing an object into a form that it can be written on stream. It's the process of converting the object into a form so that it can be stored on a file, database or memory. It is transferred across the network. Main purpose is to save the state of the object.



First, we go and import the name space for serialization.


usingSystem.Runtime.Serialization;


Then Import the formatter


using System.Runtime.Serialization.Formatters.Binary;


Convert "customer obj" to binary. Also, need the "using System.IO;"create the file or read to file. Most important, go and mark the "class".


Advantages of serialization


  1. Passing an object from one application to another.
  2. Passing an object from one domain to another.


For example:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

using System.IO;

namespace serialization

classProgram

staticvoid Main(string[] args)

//SERIALIZATION

//step 1

customer objcust = newcustomer();

objcust.CustomerName = "RAHUL";

objcust.CAddress= "VARANASI";

// object create for the binary formatter to save to file

IFormatter ibin = newBinaryFormatter();// create the binary formatter

Stream strobj = newFileStream(@"f:\MyFile.obj", //file location

FileMode.Create, // create folder

FileAccess.Write, // write the file

FileShare.None);

ibin.Serialize(strobj, objcust); //written to the file

strobj.Close();

}

}

[Serializable] // also go and mark the class

classcustomer

privatestring _CustomerName;

publicstring CustomerName

get return _CustomerName; }

set _CustomerName = value; }

}

privatestring _CAddress;

publicstringCAddress

get return _CAddress; }

set _CAddress= value; }

}

}

}


File in created to given the drive.



Deserialization


Deserialization is converting stream of byte to object. Deserialization is the reverse process of serialization. It is the process of getting back the serialization object (so that) it can be loaded into memory. It lives the state of the object by setting properties, fields etc.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

using System.IO;

namespace serialization

classProgram

staticvoid Main(string[] args)

//SERIALIZATION

//step 1

customer objcust = newcustomer();

objcust.CustomerName = "RAHUL";

objcust.CAdd = "VARANASI";

// object create for the binary formatter to save ti file

IFormatter ibin = newBinaryFormatter();// create the binary formatter

Stream strobj = newFileStream(@"f:\customer.obj", //file location

FileMode.Create, // create folder

FileAccess.Write, // write the file

FileShare.None);

ibin.Serialize(strobj, objcust); //written to the file

strobj.Close();

//DESERIALIZATION

customer objdeseri = newcustomer();

// object create for the binary formatter to save ti file

IFormatter ibindeseri = newBinaryFormatter();// create the binary formatter

Stream strobjdeseri = newFileStream(@"f:\MyFile.obj", //file location

FileMode.Open, // open the file

FileAccess.Read, // read the file

FileShare.None);

objdeseri = (customer)ibindeseri.Deserialize(strobjdeseri); //written to

the file

strobjdeseri.Close();

}

}

[Serializable] // also go and mark the class

classcustomer

privatestring _CustomerName;

publicstring CustomerName

get return _CustomerName; }

set _CustomerName = value; }

}

privatestring _CAddress;

publicstringCAddress

get return _CAddress; }

set _CAddress = value; }

}

}

}


Reverse the data from the file.



Also get to see following video on Learn C# tutorial for beginners: -


 
Rate this Article :
1 Star
2 Stars
3 Stars
4 Stars
5 Stars
1 1 1 2 1
 
 
 
 

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

Article Categories

Learn in Hindi