C Sharp Deserialization

C# Deserialization

The reverse process of serialization is known as deserialization in C# programming. Thus the object can be read from a byte stream. To deserialize the stream, we are using the BinaryFormatter.Deserialize(stream) method.

Example:

using System;  
using System.IO;  
using System.Runtime.Serialization.Formatters.Binary;  
[Serializable]  
class Employee  
{  
    public int ID;  
    public string Name;  
    public Employee(int ID, string Name)  
    {  
        this.ID = ID;  
        this.Name = Name;  
    }  
}  
public class Example  
{  
    public static void Main(string[] args)  
    {  
        FileStream str = new FileStream("d:\\file.txt", FileMode.OpenOrCreate);  
        BinaryFormatter frmtr = new BinaryFormatter();  
 
        Employee emp = (Employee)frmtr.Deserialize(str);  
        Console.WriteLine("ID: " + emp.ID);  
        Console.WriteLine("Name: " + emp.Name);  
 
        str.Close();  
    }  
}

Output:

Explanation:

In the above example, we are using the deserialization in C#.