---
title: "How do you serialize and save an object to a binary file?"  
description: "How do you serialize and save an object to a binary file?"  
author: "ICSM Computer"  
published: 2025-05-15  
updated: 2025-05-23  
canonical: https://www.mindstick.com/forum/161626/how-do-you-serialize-and-save-an-object-to-a-binary-file  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you serialize and save an object to a binary file?

How do you serialize and save an object to a [binary](https://www.mindstick.com/forum/34709/please-write-a-program-for-decimal-to-binary-conversion-in-c-sharp) [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp)?

## Replies

### Reply by Anubhav Sharma

To **serialize and save an object to a binary file** in C#, you can use the `BinaryFormatter` class (for legacy .NET Framework) or `System.Text.Json` or `System.Runtime.Serialization.Formatters.Binary` with caution.

> **Note**: `BinaryFormatter` is **obsolete and insecure** in .NET Core and .NET 5+. For newer projects, consider alternatives like `System.Text.Json`, `Json.NET`, or custom binary formats using `System.IO`.

## 1. Binary Serialization (Legacy, .NET Framework)

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

[Serializable]
public class Person
{
    public string Name;
    public int Age;
}

var person = new Person { Name = "John", Age = 30 };

using (FileStream fs = new FileStream("person.dat", FileMode.Create))
{
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(fs, person);
}
```

To **deserialize**:

```cs
using (FileStream fs = new FileStream("person.dat", FileMode.Open))
{
    BinaryFormatter formatter = new BinaryFormatter();
    Person deserializedPerson = (Person)formatter.Deserialize(fs);
    Console.WriteLine(deserializedPerson.Name);
}
```

## Warnings:

- `BinaryFormatter` is insecure for untrusted data.
- Use only for internal apps where input is trusted.

## 2. Use `System.Text.Json` (Recommended for modern apps, JSON format)

For safety and cross-platform compatibility:

```cs
using System.Text.Json;

var person = new Person { Name = "Alice", Age = 25 };
string json = JsonSerializer.Serialize(person);
File.WriteAllText("person.json", json);
```

To deserialize:

```cs
string json = File.ReadAllText("person.json");
Person person = JsonSerializer.Deserialize<Person>(json);
```

## 3. Use `FileStream` and `BinaryWriter` (Manual Binary Writing)

If you want custom binary serialization:

```cs
using (var fs = new FileStream("person.bin", FileMode.Create))
using (var writer = new BinaryWriter(fs))
{
    writer.Write("Alice");
    writer.Write(25);
}
```

And to read it back:

```cs
using (var fs = new FileStream("person.bin", FileMode.Open))
using (var reader = new BinaryReader(fs))
{
    string name = reader.ReadString();
    int age = reader.ReadInt32();
}
```

## Summary

| Method | Format | Safe for modern use | .NET Version | Notes |
| --- | --- | --- | --- | --- |
| BinaryFormatter | Binary | No | .NET Framework | Obsolete and insecure |
| System.Text.Json | JSON | Yes | .NET Core+ | Recommended |
| BinaryWriter/BinaryReader | Binary | Yes | All | Manual, needs structure |


---

Original Source: https://www.mindstick.com/forum/161626/how-do-you-serialize-and-save-an-object-to-a-binary-file

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
