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)
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:
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:
using System.Text.Json;
var person = new Person { Name = "Alice", Age = 25 };
string json = JsonSerializer.Serialize(person);
File.WriteAllText("person.json", json);
To deserialize:
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:
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:
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
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
To serialize and save an object to a binary file in C#, you can use the
BinaryFormatterclass (for legacy .NET Framework) orSystem.Text.JsonorSystem.Runtime.Serialization.Formatters.Binarywith caution.1. Binary Serialization (Legacy, .NET Framework)
To deserialize:
Warnings:
BinaryFormatteris insecure for untrusted data.2. Use
System.Text.Json(Recommended for modern apps, JSON format)For safety and cross-platform compatibility:
To deserialize:
3. Use
FileStreamandBinaryWriter(Manual Binary Writing)If you want custom binary serialization:
And to read it back:
Summary