Why is Int32 a value type?
Why does System.Int32 that derives from System.Object (thru System.ValueType) a value type while System.Objec开发者_运维知识库t is a reference type?
Because System.ValueType
is what allows us to use boxing/unboxing by overriding certain virtual members from System.Object.
http://msdn.microsoft.com/en-us/library/system.valuetype.aspx explains it pretty well:
Data types are separated into value types and reference types. Value types are either stack-allocated or allocated inline in a structure. Reference types are heap-allocated. Both reference and value types are derived from the ultimate base class Object.
In cases where it is necessary for a value type to behave like an object, a wrapper that makes the value type look like a reference object is allocated on the heap, and the value type's value is copied into it. The wrapper is marked so the system knows that it contains a value type. This process is known as boxing, and the reverse process is known as unboxing. Boxing and unboxing allow any type to be treated as an object.
Referred from https://msdn.microsoft.com/en-us/magazine/cc301569.aspx
Value type objects have two representations: an unboxed form and a boxed form. Reference types are always in a boxed form.
Value types are implicitly derived from System.ValueType. This type offers the same methods as defined by System.Object. However, System.ValueType overrides the Equals method so that it returns true if the values of the two objects' instance fields match. In addition, System.ValueType overrides the GetHashCode method so that it produces a hash code value using an algorithm that takes into account the values in the objects' instance fields. When defining your own value types, it is highly recommended that you override and provide explicit implementations for the Equals and GetHashCode methods.
精彩评论