Why classes are not serializable by default in .Net?

后端 未结 5 1585
日久生厌
日久生厌 2021-02-06 23:51

Developers have to \'opt in\' for making classes serializable by explicitly using SerializableAttribute. What could go wrong if classes were serializable by default

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 00:23

    Serializable classes imply that they have some kind of state that can be written to an external location and read again. For a lot of classes that doesn't make any sense at all - what kind of state does a Thread have, that you could successfully serialize?

    It's a little bit philosophical but by convention the default type of a class is not serializable, unless you explicitely define "this class can be serialized".

提交回复
热议问题