HI WELCOME TO SIRIS

C# Deserialization

Leave a Comment
In C# programming, deserialization is the reverse process of serialization. It means you can read the object from byte stream. Here, we are going to use BinaryFormatter.Deserialize(stream) method to deserialize the stream.
C# deserialization

C# Deserialization Example

Let's see the simple example of deserialization in C#.
  1. using System;  
  2. using System.IO;  
  3. using System.Runtime.Serialization.Formatters.Binary;  
  4. [Serializable]  
  5. class Student  
  6. {  
  7.     public int rollno;  
  8.     public string name;  
  9.     public Student(int rollno, string name)  
  10.     {  
  11.         this.rollno = rollno;  
  12.         this.name = name;  
  13.     }  
  14. }  
  15. public class DeserializeExample  
  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=(Student)formatter.Deserialize(stream);  
  23.         Console.WriteLine("Rollno: " + s.rollno);  
  24.         Console.WriteLine("Name: " + s.name);  
  25.   
  26.         stream.Close();  
  27.     }  
  28. }  
Output:
Rollno: 101
Name: sonoo

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.