Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C# Serialization & Deserialization: A Practical Example

Serialization & Deserialization in C#

Serialization and deserialization are essential when object data must move between applications or persist across sessions. In C#, serialization writes an object’s state to a storage medium—commonly a file—while deserialization reconstructs the object from that stored representation.

Consider a simple class, Tutorial, that holds two properties: ID and Name. By serializing an instance of this class to a file, we can later read the file and recreate the same object with its original data intact.

How to Serialize an Object in C#

Below is a step‑by‑step walkthrough that demonstrates the process in a console application.

  1. Create a Tutorial class with the ID and Name properties.
  2. Instantiate the class, assigning ID = 1 and Name = ".Net".
  3. Serialize the object to a file named Example.txt.
  4. Deserialize the object from the file and display its values on the console.

Paste the following code into your Program.cs file.

Step 1) Add the class. The first step is to add the class that will be serialized.

C# Serialization & Deserialization: A Practical Example

Code Explanation:

  1. The class must be marked with the [Serializable] attribute; otherwise serialization will fail.
  2. Define the Tutorial class with two fields: int ID and string Name.

Step 2) Create the object and serialize it. In this step, we instantiate the Tutorial object and write it to Example.txt.

C# Serialization & Deserialization: A Practical Example

Code Explanation:

  1. Create an instance of Tutorial and set ID = 1 and Name = ".Net".
  2. Instantiate a BinaryFormatter (note: BinaryFormatter is obsolete in .NET 5+; consider System.Text.Json or XmlSerializer for production code).
  3. Open a FileStream in FileMode.Create and FileAccess.Write to write the binary data.
  4. Call formatter.Serialize to write the object to the stream, then close the stream.

Step 3) Deserialize the object. Finally, read the data back from the file to verify the process.

C# Serialization & Deserialization: A Practical Example

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace DemoApplication
{
  [Serializable]
  class Tutorial
  {
    public int ID;
    public string Name;

    static void Main(string[] args)
    {
      Tutorial obj = new Tutorial();
      obj.ID = 1;
      obj.Name = ".Net";

      IFormatter formatter = new BinaryFormatter();
      Stream stream = new FileStream("E:\\ExampleNew.txt", FileMode.Create, FileAccess.Write);

      formatter.Serialize(stream, obj);
      stream.Close();

      stream = new FileStream("E:\\ExampleNew.txt", FileMode.Open, FileAccess.Read);
      Tutorial objnew = (Tutorial)formatter.Deserialize(stream);

      Console.WriteLine(objnew.ID);
      Console.WriteLine(objnew.Name);

      Console.ReadKey();
    }
  }
}

Code Explanation:

  1. Open the file in read mode.
  2. Deserialize the binary data back into a Tutorial instance.
  3. Print the ID and Name to the console.

Running the program in Visual Studio produces the following output:

Output:

C# Serialization & Deserialization: A Practical Example

As shown, the values were correctly retrieved from the file.

Summary

Serialization writes object data to a file, while deserialization reads that data back into an object. This fundamental technique underpins many .NET data‑exchange scenarios.

C Language

  1. C++ Classes & Objects: A Practical Guide with Code Examples
  2. C++ Struct vs Class: Key Differences, Syntax, and Best Use Cases
  3. Master C# Inheritance & Polymorphism: Practical Code Examples
  4. C# Abstract Classes: A Practical Tutorial with Code Examples
  5. Understanding C# Interfaces: Definition, Example, and Practical Use
  6. Mastering C# File Streams: StreamReader & StreamWriter – Step‑by‑Step Guide
  7. Mastering C Conditional Statements: IF, IF-ELSE, and Nested IF-ELSE Explained
  8. Understanding Java Classes and Objects: Clear Concepts, Practical Examples
  9. Python OOP Fundamentals: Classes, Objects, Inheritance, and Constructors Explained
  10. Creating ZIP Archives in Python: From Full Directory to Custom File Selection