6

In Java it is strongly recommended that all serializable classes explicitly declare serialVersionUID since the default serialVersionUID computation is highly sensitive to class details and compiler implementation is unreliable.

What's so special about serialization in C#?

Varinder Singh
  • 1,570
  • 1
  • 11
  • 25
  • Note that, in Java, it's *deliberately* unreliable. – user253751 May 12 '15 at 08:21
  • @Juan That's only relevant in Java context – Varinder Singh May 12 '15 at 08:22
  • Sorry, still sleepy this morning! – Juan May 12 '15 at 08:22
  • Suggest you read this question/answer: https://stackoverflow.com/questions/15631341/serialization-and-object-versioning-in-c-sharp – Erwin Bolwidt May 12 '15 at 09:16
  • @immibis What is achieved by making it deliberately unreliable? – Varinder Singh May 13 '15 at 05:43
  • @VarinderSingh If you screw something up, you find out about it earlier. In this case, the default is that if you change almost anything about the class, you won't be able to deserialize it. Consider if you added a field, then deserialized some instances of the class, and the field's default value is invalid. You might end up crashing hours later. – user253751 May 13 '15 at 05:50

1 Answers1

1

In .Net Serialization is less cranky than in Java.

By default it supports new fields just defaulting them, and it just ignore any data it doesn't expect.

You can still implement the same kind of version control by implementing the ISerializable interface in your class and adding your own custom VersionId to your class and check it there.

You can read more about this here

Juan
  • 3,675
  • 20
  • 34
  • 1
    Java Serialization also support addition and removal of fields. `GetField.get` methods even include an option to use a specific value if missing for custom deserialisation. The difference is, if you want to retain compatibility after you have changed a class, Java requires you to explicitly state the version. – Tom Hawtin - tackline Jul 15 '19 at 16:21