In C#, serialization refers to the transformation of an object into a byte stream, enabling it to be stored in memory, a file, or a database. The opposite procedure, deserialization, involves converting the byte stream back into an object.
Serialization is commonly employed within distributed applications.
The styling for the placeholder section is defined in the class .placeholder-diagram. This class sets the background color using a linear gradient, adds rounded corners with a border-radius of 12px, provides padding of 40px, and centers the content within the section. Additionally, it sets a margin of 20px at the top and bottom. The .placeholder-icon class within this section adjusts the font size to 3rem and adds a margin of 10px at the bottom. The text color and font size for the content inside the placeholder are defined in the .placeholder-text class, with a color of #9ca3af and a font size of 1rem.
C# SerializableAttribute
To serialize an object, it is necessary to assign the SerializableAttribute attribute to the respective type. Failure to add the SerializableAttribute attribute to the type will result in a SerializationException exception being raised during runtime.
C# Serialization example
Let's explore a basic illustration of serialization in C#. In this scenario, we will serialize an instance of the Student class. The process involves employing the BinaryFormatter.Serialize(stream, reference) technique to serialize the object.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
class Student
{
int rollno;
string name;
public Student(int rollno, string name)
{
this.rollno = rollno;
this.name = name;
}
}
public class SerializeExample
{
public static void Main(string[] args)
{
FileStream stream = new FileStream("e:\\sss.txt", FileMode.OpenOrCreate);
BinaryFormatter formatter=new BinaryFormatter();
Student s = new Student(101, "sonoo");
formatter.Serialize(stream, s);
stream.Close();
}
}
sss.txt:
JConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Student rollnoname e sonoo
To access the information, it is necessary to deserialize the serialized data from the file.