Which code should you use?

You are writing an application that uses SOAP to exchange data with other applications.
You use a Department class that inherits from ArrayList to send objects to another application.
The Department object is named dept.
You need to ensure that the application serializes the Department object for transport by using SOAP.
Which code should you use?

You are writing an application that uses SOAP to exchange data with other applications.
You use a Department class that inherits from ArrayList to send objects to another application.
The Department object is named dept.
You need to ensure that the application serializes the Department object for transport by using SOAP.
Which code should you use?

A.
SoapFormatter formatter = new SoapFormatter();
bytes[] buffer = new byte[dept.Capacity];
MemoryStream stream = new MemoryStream(buffer);
foreach (object o in dept) {
formatter.Serialize(stream, o);
}

B.
SoapFormatter formatter = new SoapFormatter();
bytes[] buffer = new byte[dept.Capacity];
MemoryStream stream = new MemoryStream(buffer);
formatter.Serialize(stream, dept);

C.
SoapFormatter formatter = new SoapFormatter();
MemoryStream stream = new MemoryStream();
foreach (object o in dept) {
formatter.Serialize(stream, o);
}

D.
SoapFormatter formatter = new SoapFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, dept);

Explanation:
Simply serialize the entire object to a stream using a SoapFormatter.
A&C attempt to serialize components of the object rather the object itself.
B attempts to serialize to an array, however the array will not be big enough to store the serialized object because it is not sized on the entire object.



Leave a Reply 0

Your email address will not be published. Required fields are marked *