Domain.com has given you the task of serializing an object and writing it to a data file using binary serialization.
You need to ensure that you meet these requirements.
What should you do?
A.
Use the following code:
object obj = new object();
Stream objSream = File.Open(“DataFile.dat”, FileMode.Create);
BinaryFormatter objFormatter = new BinaryFormatter ();
objFormatter.Serialize(objStream, obj);
B.
Use the following code:
object obj = new object();
BinaryFormatter objFormatter = new BinaryFormatter ();
objFormatter.Serialize(obj);
C.
Use the following code:
Stream objSream = File.Open(“DataFile.dat”, FileMode.Create);
BinaryFormatter objFormatter = new BinaryFormatter ();
objFormatter.Serialize(objStream);
D.
Use the following code:
object obj = new object();
Stream objSream = File.Open (“DataFile.dat”, FileMode.Create);
BinaryFormatter objFormatter = new BinaryFormatter ();
objFormatter.Serialize(obj, objStream);
Explanation:
This code instantiates an object named obj, opens a file stream, instantiates a BinaryFormatter object, and serializes the obj object to the DataFile.dat file. The File.Open method takes a file path string and FileMode enumeration value as arguments and returns a FileStream object. The Serialize method of the BinaryFormatter class takes two arguments: a stream and the object to be serialized. The Serialize method uses the stream to write the object to the destination.
Incorrect Answers:
B, C, D: If you use these options it will cause a compile-time error.
I choose A