Which code segment should you use?

You are creating a class that performs complex financial calculations.
The class contains a method named GetCurrentRate that retrieves the current interest rate and a variable named currRate
that stores the current interest rate.
You write serialized representations of the class.
You need to write a code segment that updates the currRate variable with the current interest rate
when an instance of the class is deserialized.
Which code segment should you use?

You are creating a class that performs complex financial calculations.
The class contains a method named GetCurrentRate that retrieves the current interest rate and a variable named currRate
that stores the current interest rate.
You write serialized representations of the class.
You need to write a code segment that updates the currRate variable with the current interest rate
when an instance of the class is deserialized.
Which code segment should you use?

A.
[OnSerializing]
internal void UpdateValue (StreamingContext context) {
currRate = GetCurrentRate();
}

B.
[OnSerializing]
internal void UpdateValue(SerializationInfo info) {
info.AddValue(“currentRate”, GetCurrentRate());
}

C.
[OnDeserializing]
internal void UpdateValue(SerializationInfo info) {
info.AddValue(“currentRate”, GetCurrentRate());
}

D.
[OnDeserialized]
internal void UpdateValue(StreamingContext context) {
currRate = GetCurrentRate();
}

Explanation:
A method with the OnDeserialized attribute will be called after Deserialization and any instance variables can be set.
A & B the method will fire during serializing, the question is concerned with reconstructing the object during deserialization.
C the OnDeserializing attribute is useful for default values. OnDeserializing attribute works with a method that contains a StreamContext parameter and not a SerializationInfo parameter.



Leave a Reply 1

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


seenagape

seenagape

I choose D