Updated April 12, 2023
Introduction to C# Object Serialization
To convert an object in a sequence or stream of bytes, the process used is called serialization. For the transmission of an object to the databases, file, or memory we use serialization. For the exact recreation or recovery of an object when required, serialization plays a crucial role as it saves the state of an object. Through this statement, we meant that using a web service one can transfer an object to any remote location, by a simple transfer of an object from one domain to another. The reverse process of serialization is known as de-serialization as it is the process of the conversion of a serialized byte sequence into an object.
C# Object Serialization Syntax:
For the serialization of the object in C#, an attribute called [Serializable]. If the attribute is not mentioned in a rightful manner, then at the run time a SerializableException is thrown.
Below is the syntax:
public static void SomeData()
{
string aoo = "Heyoo! Thank you for visiting us....";
FileStream boo = new FileStream(@"D:\EduCBA.txt", FileMode.Create,FileAccess.Write, FileShare.None);
BinaryFormatter coo = new BinaryFormatter();
coo.Serialize(boo, aoo);
boo.Close();
}
C# Object De-serialization Syntax:
Below is the syntax:
public static void AnotherData()
{
FileStream boo = new FileStream(@"D:\EduCBA.txt", FileMode.Open,FileAccess.Read, FileShare.Read);
BinaryFormatter doo = new BinaryFormatter();
string eoo = "";
eoo = (string)doo.Deserialize(boo);
boo.Close();
Console.WriteLine("EduCBA’s-se-ria-li-za-tion-&-de-se-ria-li-za-tion-in-C#-exam-ple");
Console.WriteLine("\n");
Console.WriteLine(eoo);
}
C# Object Serialization working with Examples
Let us discuss examples of C# Object Serialization.
Example #1
In the code below, we have to serialize the class EduCBA so we have used [Serializable]. To stop any error from occurrence after execution of the code, one should mention this attribute. After mentioning the attribute for the class that we want to serialize, we have described the four properties of our class that are “CourseName1” a string and “CoursePrice1” an integer value and similarly, “CourseName2” a string and “CoursePrice2” an integer value. To open a file “E:\EDUCBA.txt” in the read-only mode, an object hello is created an object “hello” and in the end, we have displayed the “yum” using the properties we had mentioned earlier.
Code:
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 Careerplan
{
[Serializable]
class EduCBA
{
public String CourseName1;
public int CoursePrice1;
public String CourseName2;
public int CoursePrice2;
static void Main(string[] rahul)
{
EduCBA yum = new EduCBA();
yum.CourseName1 = "C# Training";
yum.CoursePrice1 = 25900;
yum.CourseName2 = "C++ Training";
yum.CoursePrice2 = 28490;
IFormatter formatter = new BinaryFormatter();
Stream hello = new FileStream(@"E:\EDUCBA.txt",FileMode.Create,FileAccess.Write, FileShare.None);
formatter.Serialize(hello, yum);
hello.Close();
hello = new FileStream(@"E:\EDUCBA.txt",FileMode.Open,FileAccess.Read, FileShare.Read);
hello.Close();
Console.WriteLine(yum.CourseName1);
Console.WriteLine(yum.CoursePrice1);
Console.WriteLine(yum.CourseName2);
Console.WriteLine(yum.CoursePrice2);
Console.ReadKey();
}
}
}
Output:
Example #2
In the code below, we have to serialize the class CBA so we have used [Serializable]. To stop any error from occurrence after execution of the code, one should mention this attribute. After mentioning the attribute for the class that we want to serialize, we have described the nine properties of our class that are “student_ID1” an integer value and “student_name1” a string and “CGPA1” a double value that means it contains number values with decimal points and similarly “student_ID2” an integer value and “student_name2” a string and “CGPA2” a double value and “student_ID3” value is an integer and “student_name3” a string and “CGPA3” a double value. To open a file “E:\EDUCBA.txt” in the read-only mode, an object hello is created an object “learn” and in the end, we have displayed the “ID” using the properties we had mentioned earlier within different rows and text mentioning what the data actually represents.
Code:
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
namespace StudentData
{
[Serializable]
class CBA
{
public int student_ID1;
public String student_name1;
public double CGPA1;
public int student_ID2;
public String student_name2;
public double CGPA2;
public int student_ID3;
public String student_name3;
public double CGPA3;
static void Main(string[] annie)
{
CBA ID = new CBA();
ID.student_ID1 = 15023456;
ID.student_name1 = "Rahul Kashyap";
ID.CGPA1 = 9.5;
ID.student_ID2 = 18023950;
ID.student_name2 = "Ankush Rajput";
ID.CGPA2 = 8.7;
ID.student_ID3 = 19084653;
ID.student_name3 = "Aadarsh Rajput";
ID.CGPA3 = 7.5;
IFormatter eduCBA = new BinaryFormatter();
Stream learn = new FileStream(@"E:\EDUCBA.txt",FileMode.Create,FileAccess.Write, FileShare.None);
eduCBA.Serialize(learn, ID);
learn.Close();
learn = new FileStream(@"E:\EDUCBA.txt",FileMode.Open,FileAccess.Read, FileShare.Read);
learn.Close();
Console.Write("\n");
Console.Write("Welcome! Desired data is below");
Console.Write("\n");
Console.Write("\n");
Console.Write("::TABLE::");
Console.Write("\n");
Console.Write("\n");
Console.Write("::ROW1::");
Console.WriteLine("Student_ID: {0}", ID.student_ID1);
Console.Write("::ROW2::");
Console.WriteLine("Student_Name: {0}", ID.student_name1);
Console.Write("::ROW3::");
Console.WriteLine("CGPA Scored: {0}", ID.CGPA1);
Console.Write("\n");
Console.Write("\n");
Console.Write("::ROW1::");
Console.WriteLine("Student_ID: {0}", ID.student_ID2);
Console.Write("::ROW2::");
Console.WriteLine("Student_Name: {0}", ID.student_name2);
Console.Write("::ROW3::");
Console.WriteLine("CGPA Scored: {0}", ID.CGPA2);
Console.Write("\n");
Console.Write("\n");
Console.Write("::ROW1::");
Console.WriteLine("Student_ID: {0}", ID.student_ID3);
Console.Write("::ROW2::");
Console.WriteLine("Student_Name: {0}", ID.student_name3);
Console.Write("::ROW3::");
Console.WriteLine("CGPA Scored: {0}", ID.CGPA3);
}
}
}
Output:
Example #3
In the example below, for the serialization of an object firstly we have created a stream ( FileStream) object “boo” then we have created an object for BinaryFormatter object “coo” then we have called “coo.Serialize(boo, aoo)” which is a BinaryFormatter.Serialize() method for the serialization of an object in C#.
Similarly for the de-serialization of an object firstly we have created a stream ( FileStream) object “boo” which is used for reading the serialized output then we have created an object for BinaryFormatter object “doo” then we have called “doo. Deserialize(boo)” which is a BinaryFormatter.Deserialize() method for the de-serialization of an object in C#.
Code:
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace EDUCBA
{
class Rahul
{
public static void SomeData()
{
string aoo = "Heyoo! Thank you for visiting us....";
FileStream boo = new FileStream(@"D:\EduCBA.txt", FileMode.Create,FileAccess.Write, FileShare.None);
BinaryFormatter coo = new BinaryFormatter();
coo.Serialize(boo, aoo);
boo.Close();
}
public static void AnotherData()
{
FileStream boo = new FileStream(@"D:\EduCBA.txt", FileMode.Open,FileAccess.Read, FileShare.Read);
BinaryFormatter doo = new BinaryFormatter();
string eoo = "";
eoo = (string)doo.Deserialize(boo);
boo.Close();
Console.WriteLine("EduCBA’s-se-ria-li-za-tion-&-de-se-ria-li-za-tion-in-C#-exam-ple");
Console.WriteLine("\n");
Console.WriteLine(eoo);
}
static void Main(string[] foo)
{
SomeData();
AnotherData();
Console.ReadLine();
}
}
}
Output:
Conclusion
On the basis of above discussion, we understood the serialization of an object in C# as well as de-serialization of the same object in C#. We also understood the importance of serialization. We have discussed various examples related to C# serialization and C# de-serialization along with the syntaxes of both.
Recommended Articles
This is a guide to C# Object Serialization. Here we also discuss the introduction, syntax, parameters, and C# Object Serialization working with different examples. You may also have a look at the following articles to learn more –