I am implementing the ISerializable
interface for my class.
I have a constant variable in the class like:
public const decimal Cost = 3.2M
When I implement the GetObjectData
method, can I / should I serial this variable?
When I implement the GetObjectData method, can I / should I serial this variable?
Absolutely not.
Even if you did write it out, it's not like you could change the value of the constant when you read it back in again.
More generally, you shouldn't serialize static fields at all (and const
implies static
). Anything static is not part of the state of an instance, and it's the data within the instance that you're trying to serialize.
See more on this question at Stackoverflow