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

后端 未结 6 1838
闹比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 18:08

    A boxed value type is effectively a reference type (it walks like one and quacks like one, so effectively it is one). I would suggest that ValueType isn't really the base type of value types, but rather is the base reference type to which value types can be converted when cast to type Object. Non-boxed value types themselves are outside the object hierarchy.

提交回复
热议问题