How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes?

后端 未结 6 1840
闹比i
闹比i 2020-11-22 17:20

C# doesn\'t allow structs to derive from classes, but all ValueTypes derive from Object. Where is this distinction made?

How does the CLR handle this?

6条回答
  •  长发绾君心
    2020-11-22 17:46

    This is a somewhat artificial construct maintained by the CLR in order to allow all types to be treated as a System.Object.

    Value types derive from System.Object through System.ValueType, which is where the special handling occurs (ie: the CLR handles boxing/unboxing, etc for any type deriving from ValueType).

提交回复
热议问题