Do value types (Integer, Decimal, Boolean, etc…) inherit from Object?

前端 未结 8 2047
梦谈多话
梦谈多话 2020-12-03 14:59

Looking for clarification on this...

I\'ve heard that \'everything\' in .Net inherits from Object. I\'ve also heard that the difference between value types and refe

相关标签:
8条回答
  • 2020-12-03 15:06

    value types inherit (indirectly) from object

    ... but not everything in .net inherits from object.

    0 讨论(0)
  • 2020-12-03 15:07

    Value types also inherit from Object, but not directly. They inherit from ValueType, which in turn inherits Object.

    0 讨论(0)
  • 2020-12-03 15:08

    According to Red Gate's .NET Reflector they inherit (indirectly) from object.

    • Object -> ValueType -> int32
    • Object -> ValueType -> boolean
    • Object -> ValueType -> decimal
    • Object -> ValueType -> byte
    • Object -> ValueType -> char
    • Object -> ValueType -> uint32

    I haven't checked other types, but it would seem they do. I would highly recommend getting Reflector - it's a free download and it will help you answer countless other questions about how various parts of the .NET framework are coded. Some days I wonder how I'd live without it.

    The greatest thing about Reflector is that you don't need to rely on someone's potentially outdated (or incorrect or badly interpreted) writing to discover what is really going on inside the .NET Framework - including that on MSDN - not even the almighty Microsoft is infallible. The documentation is only as current as its last modification. Getting your answers directly from the code is the least likely to be incorrect - assuming of course that you're able to correctly interpet said code ;)

    0 讨论(0)
  • 2020-12-03 15:16

    In short, not quite everything derives from object, there are exceptions. This blog post from Eric Lippert is probably the best reference on this topic: "Not everything derives from object"

    All structs implicitly derive from System.ValueType.

    The difference between value types and reference types is a semantical issue: value types exhibit value semantics while reference types exhibit value semantics. Implementation details (such as where they are allocated etc. are not important).

    ints, for example, are value types because they are structs. Of course, we model ints as value types because they represent values and we want value semantics, not reference semantics.

    0 讨论(0)
  • 2020-12-03 15:18

    Yes, value types do inherit from Object.

    See the Inheritance Hierarchy section here: http://msdn.microsoft.com/en-us/library/system.valuetype.aspx

    The Remarks section in the same page says, literally:

    Both reference and value types are derived from the ultimate base class Object.

    0 讨论(0)
  • 2020-12-03 15:19

    from MSDN: (http://msdn.microsoft.com/en-us/library/s1ax56ch%28VS.71%29.aspx)

    Value Types

    The value types consist of two main categories:

    * Struct type
    * Enumeration type
    

    The struct types contain the user-defined struct types and the following built-in simple types:

    * Numeric types
          o Integral types
          o Floating-point types
          o decimal
    * bool
    

    Main Features of Value Types

    A variable of a value type always contains a value of that type. The assignment to a variable of a value type creates a copy of the assigned value, while the assignment to a variable of a reference type creates a copy of the reference but not of the referenced object.

    All value types are derived implicitly from the Object class.

    Unlike reference types, it is not possible to derive a new type from a value type. However, like reference types, structs can implement interfaces.

    Unlike reference types, it is not possible for a value type to contain the null value.

    Each value type has an implicit default constructor that initializes the default value of that type. For information on default values of value types, see Default Values Table.

    Main Features of Simple Types

    All of the simple types are aliases of the .NET Framework System types. For example, int is an alias of System.Int32. For a complete list of aliases, see Built-in Types Table.

    Constant expressions, whose operands are all simple type constants, are evaluated at compilation time. For more information, see 7.15 Constant expressions.

    Simple types can be initialized using literals. For example, 'A' is a literal of the type char and 2001 is a literal of the type int.

    0 讨论(0)
提交回复
热议问题