Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we need not to declare a serialVersionUID(or equivalent) in C#?

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#?

like image 706
Varinder Singh Avatar asked May 12 '15 08:05

Varinder Singh


People also ask

What would happen if you don't define serialVersionUID?

If we don't define a serialVersionUID state for a Serializable class, then Java will define one based on some properties of the class itself such as the class name, instance fields, and so on.

What will happen if I will not declare serialVersionUID in a class which implements serializable?

If a serializable class does not explicitly declare a serialVersionUID , then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification.

What is a serialVersionUID and why should I use it?

SerialVersionUID is a unique identifier for each class, JVM uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization. Specifying one gives more control, though JVM does generate one if you don't specify.

What happens if two classes have same serialVersionUID?

The serialVersionUID is specific to a class, not an object, since they are static. I don't think it matters if two different classes have the same serialVersionUID, since it is used in deserializing objects of the same class.


1 Answers

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

like image 113
Juan Avatar answered Oct 30 '22 10:10

Juan