10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

Serialization in C#

Updated on Sep 3, 2025
 
46,004 Views

The process of serialization involves the conversion of any object into a stream of bytes. This is done to transmit the object to the memory, file or database. The reverse of this process is known as deserialization as it involves reading the object from the stream of bytes.

Serialization is quite important as it saves the state of the object so that it can be recovered or recreated as and when it is required. This means that the object can be transferred to any remote location as required using a web service by transferring the object from one domain to another.

Example of Serialization

The SerializableAttribute is applied is applied to serialize the object. If this is not done correctly then a SerializableException is thrown at the run-time.

A program that demonstrates Serialization is given as follows:

using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace SerializationDemo
{
 [Serializable]
 class Student
 {
   public int rno;
   public String name;
   public double marks;
   static void Main(string[] args)
   {
     Student s1 = new Student();
     s1.rno = 1;
     s1.name = "John Smith";
     s1.marks = 87.5;
     IFormatter f = new BinaryFormatter();
     Stream str = new FileStream(@"E:\SerializableFile.txt",FileMode.Create,FileAccess.Write);
     f.Serialize(str, s1);
     str.Close();
     str = new FileStream(@"E:\SerializableFile.txt",FileMode.Open,FileAccess.Read);
     Student s2 = (Student)f.Deserialize(str);
     Console.WriteLine("Roll Number: {0}", s2.rno);
     Console.WriteLine("Name: {0}", s2.name);
     Console.WriteLine("Marks: {0}", s2.marks);
 }
}
}

Source Code: Program to implement Serialization in C#

The output of the above program is as follows:

Roll Number: 1
Name: John Smith
Marks: 87.5

Now let us understand the above program.

The class Student contains the student roll number, name and marks. The code snippet for this is given as follows:

public int rno;
public String name;
public double marks;

The function Main() contains the object s1 of Student. Then the values of s1 are initialized. The code snippet for this is given as follows:

Student s1 = new Student();
     s1.rno = 1;
     s1.name = "John Smith";
     s1.marks = 87.5;

The contents of s1 are stored in the file SerializableFile.txt using serialization and then the file stream is closed. After that the file stream is opened again and the contents of the file are stored in object s2 of class Student. The code snippet for this is given as follows:

 IFormatter f = new BinaryFormatter();
     Stream str = new FileStream(@"E:\SerializableFile.txt",FileMode.Create,FileAccess.Write);
     f.Serialize(str, s1);
     str.Close();
     str = new FileStream(@"E:\SerializableFile.txt",FileMode.Open,FileAccess.Read);
     Student s2 = (Student)f.Deserialize(str);

Finally, the roll number, name and marks stored in s2 are displayed. The code snippet for this is given as follows:

Console.WriteLine("Roll Number: {0}", s2.rno);
     Console.WriteLine("Name: {0}", s2.name);
     Console.WriteLine("Marks: {0}", s2.marks);
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses