I recently had a discussion with a colleague about serialization of byte data over a network.
He used the BinaryFormatter class to “unparse” the byte data I was sending to him. This did not work and he obviously had exceptional… exceptions. Binaryformatter could not “unparse” the data correctly since my data was simply a byte array.
His motivation for BinaryFormatter was platform independence. I am not persuaded of such a stance. When we both used BinaryReader or BinaryWriter, things worked well in code land.
What is the use therefore of BinaryFormatter and should I look at using it in each scenario wherever I need to send bytes over the wire?
Definitely, if you have your data as
byte[],BinaryFormatteris not a wise thing to use. You just write the data out to the wire. However, if you have a set of objects and want to serialize them to a stream,BinaryFormatteris much easier to use than manually writing each field of each type by hand. The purpose ofBinaryFormatteror any serializer/deserializer scheme in general is to provide a way to persist an object graph (possibly complex) as a sequence of bytes.