Difference between BinaryWriter and BinaryFormatter.Serialize?
Asked Answered
H

2

6

I'm new to object serialization, and in the course of my learning how to read from and write to a file (deserialize and serialize) using BinaryFormatter, I came across BinaryReader and BinaryWriter, which seemed to be doing the same thing.

Is there some subtle difference between BinaryFormatter.Serialize() and BinaryWriter? Or is BinaryWriter just a more compact way performing the the same action as BinaryFormatter.Serialize()?

Halvah answered 22/11, 2016 at 19:16 Comment(2)
Potential codeproject article that might be of some use to you BinaryFormatter vs. Manual SerializingSupination
Just to update anyone that may come across this, BinaryFormatter is now considered "dangerous" to use if user input is involved. For more details: learn.microsoft.com/en-us/dotnet/standard/serialization/…Sinuosity
D
10

BinaryWriter is used to write primitive types in binary to a stream and supports writing strings in a specific encoding. BinaryFromatter is responsible for serializing an entire object or graph of connected objects into binary format. So, I suppose you can say BinaryWriter is a much more elementary form of something like BinaryFormatter.

I got this information here: BinaryWriter & BinaryFormatter

Defoe answered 22/11, 2016 at 19:30 Comment(0)
C
8

BinaryWriter and BinaryFormatter are two different thing.

BinaryFormatter is used for serialization. It helps you to map a C# object to a binary representation which you can write to a file, a network stream etc.

But BinaryWriter does not help you map the C# object to binary data. It just gives you the ability to write binary data (as the name implies). So you give it primitive types like an int, it converts it into binary and write it. After writing when you need reading it you have to use a BinaryReader and you must know somehow that you have to read an int. So you have to serialize your data somehow yourself.

You can say BinaryFormatter uses BinaryWriter to be able to write binary data but it does a lot of other jobs to automatically serialize and deserialize your object.

Cheque answered 22/11, 2016 at 19:27 Comment(2)
"BinaryWriter does not help you map the C# object to binary data" --- That's actually exactly how it helps. Corey Berigan's answer is spot on. Both BinaryWriter and BinaryFormatter are used to serialize objects as bytes and write those bytes to a stream. The difference is that BinaryWriter is intended to serialize primitives in a way that can be later read by BinaryReader. BinaryFormatter is intended for serializing more complex object graphs, where instances are marked [Serializable]. "BinaryFormatter uses BinaryWriter to be able to write binary data" -- Are you sure? I'd be surprised.Kistler
@RonnieOverby , I think u misunderstood me, By C# Object I meant a complex object not a primitive as I have mentioned that you can write primitive objects to binary using BinaryWriter. BinaryFormatter do use BinaryWriter. It's simple and obvious actually cause finally it should break down complex object to primitives and then write them. You can see the implementation although to become sure: github.com/dotnet/corefx/blob/…Cheque

© 2022 - 2024 — McMap. All rights reserved.