BinaryFormatter

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

Is it any advantage to use one over the other between these
two.
IFormatter serializer = new BinaryFormatter();
or
BinaryFormatter serializer = new BinaryFormatter();

If I have this
FileStream saveFile = new FileStream ("MyFile.bin", FileMode.Create,
FileAccess.Write);
serializer.serialize(saveFile, myObjectToSerialize);
saveFile.Close();

//Tony
 
Tony said:
Hi!

Is it any advantage to use one over the other between these
two.
IFormatter serializer = new BinaryFormatter();
or
BinaryFormatter serializer = new BinaryFormatter();

If I have this
FileStream saveFile = new FileStream ("MyFile.bin", FileMode.Create,
FileAccess.Write);
serializer.serialize(saveFile, myObjectToSerialize);
saveFile.Close();

//Tony

None whatsoever.
 
Is it any advantage to use one over the other between these
two.
IFormatter serializer = new BinaryFormatter();
or
BinaryFormatter serializer = new BinaryFormatter();

If I have this
FileStream saveFile = new FileStream ("MyFile.bin", FileMode.Create,
FileAccess.Write);
serializer.serialize(saveFile, myObjectToSerialize);
saveFile.Close();

The lines will execute the exact same code.

If all the code is inside a method so that serializer
is a local variable, then you can throw a coin to
pick the one you want to use.

But if serializer is a field or property or being
passed as an argument or being returned from a method,
then you should prefer the IFormatter version.

In that case the compiler will verify that you only
use functionality defined by the IFormatter interface,
which means that you can switch to a different
formatter just by changing a single place in the code.

Arne
 
Well you definitely want to avoid disk accesses for performance reasons. So the first set of calls probably has better performance since you are not writing to or reading from the disk.
-- Adam
 
Back
Top