What is Serialization in C#?
Serialization in C# is the 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; or, it can be transferred across the network. Its main purpose is to save the state of the object so that it can be recreated when needed.
What is Deserialization in C#?
As the name suggests, deserialization in C# is the reverse process of serialization. It is the process of getting back the serialized object so that it can be loaded into memory. It resurrects the state of the object by setting properties, fields etc.
Types
- Binary Serialization
- XML Serialization
- JSON Serialization
Example
Here I will be giving you an example of how to serialize and deserialize an object using binary formatter or xml formatter.
Create a new Windows Form Application and add few controls to it as shown below.

Now make a class named Employee:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
[Serializable]
public class Employee
{
private string Name;
public string name
{
get
{
return Name;
}
set
{
Name = value;
}
}
private string Phone;
public string phone
{
get
{
return Phone;
}
set
{
Phone = value;
}
}
private DateTime DoB;
public DateTime dob
{
get
{
return DoB;
}
set
{
DoB = value;
}
}
private string Department;
public string department
{
get
{
return Department;
}
set
{
Department = value;
}
}
private int Salary;
public int salary
{
get
{
return Salary;
}
set
{
Salary = value;
}
}
[NonSerialized]
public string additionalInfo;
}
}
Put [Serializable] on top of the class. For those attributes which you don't want to serialize put [NonSerialized] on them.
Add a click event for Serialize button. When clicking on the serialize button we want to serialize the object of class Employee and store it in a file named "employee.binary".





FredPosted Dec 22, 2025, 10:33 AM
Hi, could you or anyone please confirm if, few months ago pointed by this very URL, there was a ser/des example written in C# specific for Json? And if yes is any way possible to get it back?
Charles HildebrantPosted Jul 7, 2023, 5:58 PM
Hi, Fahad, to get this to work I had to suppress a warning: #pragma warning disable SYSLIB0011 // Type or member is obsolete bf.Serialize(fsout, employee); #pragma warning restore SYSLIB0011 // Type or member is obsolete You wrote this just last month but the files shown are from 2016. What version of .net did you use? I'll research an update. Cheers Charles
Anand NavalePosted Nov 29, 2019, 4:20 AM
Good One...!
Sweety KumariPosted Oct 5, 2018, 4:36 AM
Hi pls anyone explain me private void Serialize_Click(object sender, EventArgs e) { Employee emp = new Employee { name = textBoxName.Text, phone = textBoxPhone.Text, dob = dateTimePickerDoB.Value, department = textBoxDepartment.Text, ... Doesnt look like constructor. Then what use of this code ?TIA
Hitanshi MehtaPosted Sep 28, 2018, 6:27 PM
Good one!!!!!!
Chaithanya ShivaPosted Feb 1, 2018, 9:17 AM
This article is good but if you enter multiple values the old values are getting overriden by new values
VIKAS KUMARPosted Nov 22, 2016, 4:42 AM
Nice and very effective of explaining.
Maaz MahmoodPosted Jul 1, 2016, 4:05 PM
Good Way
ibrahim shaikPosted Jun 28, 2016, 11:08 AM
Can you pls extend the article for json serialization as well.Thanks in Advance...
Vignesh ManiPosted Jun 28, 2016, 8:28 AM
Nice
Suresh MolabantiPosted Jun 28, 2016, 5:49 AM
It's awesome thank you
Suresh MolabantiPosted Jun 28, 2016, 5:48 AM
Bro it is superb...........
Samir BhogaytaPosted Jun 27, 2016, 5:55 AM
It's nice one Fahad.....Thanks
Basil HowitsonPosted Jun 27, 2016, 4:43 AM
Thank you Fahad. This is a very clear and understandable explanation that actually makes sense to me.