.Net Framework
C# Tutorial
C# Control Statement
C# Arrays
C# Object Class
C# Properties
C# Inheritance
C# Polymorphism
C# Abstraction
C# Strings
C# Exception Handling
C# File IO
C# Collections
C# Generics
C# Delegates
C# Reflection
Anonymous Function
C# Multithreading
C# Synchronization
C# Web Service
C# Misc
C# New Features
C# Programs
C# Interview Questions
ADO.NET Tutorial
ASP.NET Tutorial
C# Deserialization
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 Example
Let's see the simple example of deserialization in C#.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
class Student
{
public int rollno;
public string name;
public Student(int rollno, string name)
{
this.rollno = rollno;
this.name = name;
}
}
public class DeserializeExample
{
public static void Main(string[] args)
{
FileStream stream = new FileStream("e:\\sss.txt", FileMode.OpenOrCreate);
BinaryFormatter formatter=new BinaryFormatter();
Student s=(Student)formatter.Deserialize(stream);
Console.WriteLine("Rollno: " + s.rollno);
Console.WriteLine("Name: " + s.name);
stream.Close();
}
}
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
class Student
{
public int rollno;
public string name;
public Student(int rollno, string name)
{
this.rollno = rollno;
this.name = name;
}
}
public class DeserializeExample
{
public static void Main(string[] args)
{
FileStream stream = new FileStream("e:\\sss.txt", FileMode.OpenOrCreate);
BinaryFormatter formatter=new BinaryFormatter();
Student s=(Student)formatter.Deserialize(stream);
Console.WriteLine("Rollno: " + s.rollno);
Console.WriteLine("Name: " + s.name);
stream.Close();
}
}
Output:
Rollno: 431
Name: sachin
Name: sachin