Tuesday, 22 January 2019

Serialization in c#

In C#, serialization is the process of converting object into byte stream so that it can be saved to memory, file or database. The reverse process of serialization is called deserialization.


Example:
  1. using System;  
  2. using System.IO;  
  3. using System.Runtime.Serialization.Formatters.Binary;  
  4. [Serializable]  
  5. class Student  
  6. {  
  7.     int rollno;  
  8.     string name;  
  9.     public Student(int rollno, string name)  
  10.     {  
  11.         this.rollno = rollno;  
  12.         this.name = name;  
  13.     }  
  14. }  
  15. public class SerializeExample  
  16. {  
  17.     public static void Main(string[] args)  
  18.     {  
  19.         FileStream stream = new FileStream("e:\\sss.txt", FileMode.OpenOrCreate);  
  20.         BinaryFormatter formatter=new BinaryFormatter();  
  21.           
  22.         Student s = new Student(101, "sonoo");  
  23.         formatter.Serialize(stream, s);  
  24.   
  25.         stream.Close();  
  26.     }  
  27. }  

No comments:

Post a Comment