Rob P.
Rob P.

Reputation: 15071

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

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 reference types is that reference types inherit from Object and value types do not.

My understanding was that everything was an object (inherits from System.Object); but value types and reference types were simply 'different' from one another. Value types are allocated on the stack and reference types get a 'pointer' placed on the Stack that points to an address on the Heap.

Is that the gist of it? What makes an Integer a value type? That's something inherent in the language?

Upvotes: 17

Views: 8525

Answers (8)

eglasius
eglasius

Reputation: 36027

value types inherit (indirectly) from object

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

Upvotes: 4

Broam
Broam

Reputation: 4648

Value types, such as Int32, are structs.

From the VS 2008 C# help file (since I had it open) on structs:

A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object.

Upvotes: 20

BenAlabaster
BenAlabaster

Reputation: 39836

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 ;)

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1500245

It depends on how you view the terminology - which depends on whether you're talking about C# or the CLI spec. For example, in the CLI spec (ECMA-355) sections 8.9.8 and 8.9.10 state:

Value types do not inherit, although the associated boxed type is an object type and hence inherits from other types.

and

In their unboxed form value types do not inherit from any type. Boxed value types shall inherit directly from System.ValueType unless they are enumerations, in which case, they shall inherit from System.Enum. Boxed value types shall be sealed.

So from the CLI's point of view, the answer to the question is no.

However, let's look at the C# spec - and as we're in an ECMA-like mood, let's go for that version (which is currently stuck at C# 2). Section 11.1.1 states:

All value types implicitly inherit from the class System.ValueType, which, in turn, inherits from class object.

So from the C# specification's point of view, the answer is yes.

One could argue that you tagged your question ".net" so we should use the CLI definition; if you'd tagged it "c#" we should have used the C# definition. See how arbitrary it is? :)

All of this spec-diving isn't to much practical purpose though. The answer depends on the intricacies of definitions. It's more sensible to construct some interesting situation where it matters... so what do you want to do? If you can present some code, we can answer questions about what will happen - and that's more important than definitions.

(Yes, this is unusual for me - in general, terminology matters a lot to me. In some cases, however, the subtleties are more of a curse than a blessing.)

Upvotes: 23

CesarGon
CesarGon

Reputation: 15325

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.

Upvotes: 3

dtroy
dtroy

Reputation: 1227

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.

Upvotes: 2

jason
jason

Reputation: 241621

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.

Upvotes: 2

Fredrik Mörk
Fredrik Mörk

Reputation: 158309

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

Upvotes: 4

Related Questions