Sunday, February 4, 2018

You can add fields to a new version of a DataContract, but they might not be initialized

.NET DataContracts are a convenient way to serialize objects as XML. Sometimes it's necessary to add a field to a serialized data structure. This keeps compatibility with older serialized strings, but one thing to watch out for is that the new field may not be initialized at all when loading an old string. Even if the field is explicitly given a default value when defining the type, it comes out null when loading a serialized string that doesn't include that field, so there could be some null reference surprises.

No comments:

Post a Comment