top
April flash sale

Search

C# Tutorial

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 SerializationThe 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:Source Code: Program to implement Serialization in C#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);  } } }The output of the above program is as follows:Roll Number: 1 Name: John Smith Marks: 87.5Now 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);
logo

C# Tutorial

Serialization in C#

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:

Source Code: Program to implement Serialization in C#

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);
 }
}
}

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);

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

austin faith

Avery good write-up. Please let me know what are the types of C# libraries used for AI development.

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial