Serialization in C#
Definition:
Process to convert a object into stream of data into memory cache/ physical file/ database
Usage: For Maintain/Preserve data present inside a class will be store for future usage/ reference using Serialization for easy understanding below sample
Sample 1:
/* This class information will be serialized and stored in the file Serializable attribute above the class name will make all fields present inside the class to be serialized if you need to omit any variable from serializing you need to placed [NonSerializable()] attribute above the field operaion* /
[Serializable()]
public class employeeInfo
{
public string empName {get;set;}
public int empId {get; set;}
public string designation {get; set;}
public employeeInfo (string name, int id, string role)
public class mainprogram
{
static void main ()
{
Console.WriteLine("Provide your Option 1 -Read or 2- Write");
switch( Console.Readline())
{
case "1":
using (Stream stream= File.Open("data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
var empList = (List <employeeInfo)bin.Deserialize(stream);
foreach(employeeInfo emp in empList)
{
Console.WriteLine("{0},{1},{2}",
employeeInfo.empName, employeeInfo.empId, employeeInfo.designation);
}
}
}
break;
case "2":
var emplList = new List<Lizard>();
break;
}
}
}
Definition:
Process to convert a object into stream of data into memory cache/ physical file/ database
Usage: For Maintain/Preserve data present inside a class will be store for future usage/ reference using Serialization for easy understanding below sample
Sample 1:
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
/* This class information will be serialized and stored in the file Serializable attribute above the class name will make all fields present inside the class to be serialized if you need to omit any variable from serializing you need to placed [NonSerializable()] attribute above the field operaion* /
[Serializable()]
public class employeeInfo
{
public string empName {get;set;}
public int empId {get; set;}
public string designation {get; set;}
public employeeInfo (string name, int id, string role)
{
empName =Name;
empId = id;
designation = role;
}
}public class mainprogram
{
static void main ()
{
Console.WriteLine("Provide your Option 1 -Read or 2- Write");
switch( Console.Readline())
{
case "1":
using (Stream stream= File.Open("data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
var empList = (List <employeeInfo)bin.Deserialize(stream);
foreach(employeeInfo emp in empList)
{
Console.WriteLine("{0},{1},{2}",
employeeInfo.empName, employeeInfo.empId, employeeInfo.designation);
}
}
}
break;
case "2":
break;
case "2":var emplList = new List<Lizard>();
emplList.Add(new employeeInfo("mponraj",846281, "Developer")); emplList.Add(new employeeInfo("msadasi",853909, "Developer")); emplList.Add(new employeeInfo("msriniv",828467, "Developer")); using (Stream stream = File.Open("data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, emplList);
}
}
break;
default:break;
}
}
}