Nick C
2009-10-08 15:58:53 UTC
Hi,
I was having a discussion with some co-workers today about the
distinction between value types and objects - the discussion centred
around whether or not value types were objects. The elements adding to
our confusion were that:
* value types (which are defined as structs) inherit from the
System.ValueType class, which in turn inherits from System.Object,
which would imply that they are in fact objects
* the CLR spec isn't very clear on the matter (or at least, it wasn't
to us)
This is what I think the CLR spec says (partition 1, pages 18-20,
corresponding to pages 30-32 in the PDF at
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-335.pdf):
* value types are not objects, because objects are 'reference type of
a self-describing value [p18]' (and value types aren't reference
types)
* boxing is used to convert a value type to an object; this is handled
completely transparently by the CLR (unlike Java for example), in that
the 'boxed type cannot be directly referred to by name [p20]' (i.e. a
developer can't create a value of the boxed type directly; the boxed
type is in effect hidden to end users)
* the types in the standard library deriving from System.ValueType
(e.g. System.Int32) describes the value type (as opposed to implying a
reference); however, any methods defined in the value type will be
applied to the object resulting from the boxing operation (i.e. boxed
reference type, as opposed to the value type), because any value type
is boxed before a method defined in its type (which derives from
System.ValueType) is called on it.
Is this correct?
This would mean that:
* value types are not objects as per the CLR at the point of
allocation, even though they inherit from System.ValueType which in
turn inherits from System.Object
* however, they are boxed to objects as soon as a method defined in
their type is called on them, which sort of explains why
System.ValueType inherits from System.Object
* a class definition doesn't necessarily imply an object (as in
System.ValueType, which is a class rather than a struct)
* to summarize, even though value types (such as structs, which
according to MSDN forbid inheritance) inherit from System.Object, they
aren't in fact objects (until they get autoboxed).
Any further clarification greatly appreciated.
Thanks,
Nick
I was having a discussion with some co-workers today about the
distinction between value types and objects - the discussion centred
around whether or not value types were objects. The elements adding to
our confusion were that:
* value types (which are defined as structs) inherit from the
System.ValueType class, which in turn inherits from System.Object,
which would imply that they are in fact objects
* the CLR spec isn't very clear on the matter (or at least, it wasn't
to us)
This is what I think the CLR spec says (partition 1, pages 18-20,
corresponding to pages 30-32 in the PDF at
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-335.pdf):
* value types are not objects, because objects are 'reference type of
a self-describing value [p18]' (and value types aren't reference
types)
* boxing is used to convert a value type to an object; this is handled
completely transparently by the CLR (unlike Java for example), in that
the 'boxed type cannot be directly referred to by name [p20]' (i.e. a
developer can't create a value of the boxed type directly; the boxed
type is in effect hidden to end users)
* the types in the standard library deriving from System.ValueType
(e.g. System.Int32) describes the value type (as opposed to implying a
reference); however, any methods defined in the value type will be
applied to the object resulting from the boxing operation (i.e. boxed
reference type, as opposed to the value type), because any value type
is boxed before a method defined in its type (which derives from
System.ValueType) is called on it.
Is this correct?
This would mean that:
* value types are not objects as per the CLR at the point of
allocation, even though they inherit from System.ValueType which in
turn inherits from System.Object
* however, they are boxed to objects as soon as a method defined in
their type is called on them, which sort of explains why
System.ValueType inherits from System.Object
* a class definition doesn't necessarily imply an object (as in
System.ValueType, which is a class rather than a struct)
* to summarize, even though value types (such as structs, which
according to MSDN forbid inheritance) inherit from System.Object, they
aren't in fact objects (until they get autoboxed).
Any further clarification greatly appreciated.
Thanks,
Nick