codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

32 / 60 topics
30File I/O Operations in C#31Streaming in C#32Serialization in C#
Tutorials/C# Programming/Serialization in C#
🔷C# Programming

Serialization in C#

Updated 2026-05-15
10 min read

Serialization in C#

Introduction

Serialization is the process of converting an object into a format that can be stored or transmitted, and deserialization is the reverse process. In C#, serialization is often used to save objects to files, send them over a network, or store them in databases. This tutorial will guide you through the basics of serialization in C# using file handling.

Concept

Serialization allows you to convert an object into a byte stream, which can then be saved to a file or transmitted over a network. Deserialization converts the byte stream back into an object. C# provides several ways to perform serialization, including binary serialization and XML serialization. In this tutorial, we will focus on binary serialization using the BinaryFormatter class.

Examples

Step 1: Define a Serializable Class

First, you need to define a class that you want to serialize. The class must be marked with the [Serializable] attribute.

csharp
1using System;
2
3[Serializable]
4public class Person
5{
6 public string Name { get; set; }
7 public int Age { get; set; }
8
9 public Person(string name, int age)
10 {
11 Name = name;
12 Age = age;
13 }
14}

Step 2: Serialize an Object to a File

To serialize an object to a file, you can use the BinaryFormatter class. Here's how you can do it:

csharp
1using System;
2using System.IO;
3using System.Runtime.Serialization.Formatters.Binary;
4
5public static void SerializeObjectToFile(Person person, string filePath)
6{
7 using (FileStream fs = new FileStream(filePath, FileMode.Create))
8 {
9 BinaryFormatter formatter = new BinaryFormatter();
10 formatter.Serialize(fs, person);
11 }
12}

Step 3: Deserialize an Object from a File

To deserialize an object from a file, you can use the BinaryFormatter class again. Here's how you can do it:

csharp
1using System;
2using System.IO;
3using System.Runtime.Serialization.Formatters.Binary;
4
5public static Person DeserializeObjectFromFile(string filePath)
6{
7 using (FileStream fs = new FileStream(filePath, FileMode.Open))
8 {
9 BinaryFormatter formatter = new BinaryFormatter();
10 return (Person)formatter.Deserialize(fs);
11 }
12}

Step 4: Putting It All Together

Here's a complete example that demonstrates how to serialize and deserialize a Person object:

csharp
1using System;
2
3class Program
4{
5 static void Main()
6 {
7 Person person = new Person("John Doe", 30);
8 string filePath = "person.dat";
9
10 // Serialize the object to a file
11 SerializeObjectToFile(person, filePath);
12 Console.WriteLine("Object serialized successfully.");
13
14 // Deserialize the object from the file
15 Person deserializedPerson = DeserializeObjectFromFile(filePath);
16 Console.WriteLine($"Deserialized Object: Name = {deserializedPerson.Name}, Age = {deserializedPerson.Age}");
17 }
18
19 public static void SerializeObjectToFile(Person person, string filePath)
20 {
21 using (FileStream fs = new FileStream(filePath, FileMode.Create))
22 {
23 BinaryFormatter formatter = new BinaryFormatter();
24 formatter.Serialize(fs, person);
25 }
26 }
27
28 public static Person DeserializeObjectFromFile(string filePath)
29 {
30 using (FileStream fs = new FileStream(filePath, FileMode.Open))
31 {
32 BinaryFormatter formatter = new BinaryFormatter();
33 return (Person)formatter.Deserialize(fs);
34 }
35 }
36}

Output

Output
Object serialized successfully.
Deserialized Object: Name = John Doe, Age = 30

What's Next?

In the next section, we will explore collections in C#, including arrays, lists, and dictionaries. Understanding how to work with collections is essential for building robust applications.


This tutorial should give you a solid understanding of how to serialize and deserialize objects in C# using file handling. Remember that binary serialization is just one way to perform serialization, and there are other methods available depending on your needs.


PreviousStreaming in C#Next Collections in C#

Recommended Gear

Streaming in C#Collections in C#