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?
All the classes in Java are inherited from the Object class. It is the parent class in Java. All other classes directly or indirectly inherit the Object class. Inheritance is an object-oriented concept in which one class uses the properties and behavior of another class.
No, primitive types do not inherit from Object since they're not classes.
Value types include the following: All numeric data types. Boolean , Char , and Date. All structures, even if their members are reference types.
A value type instance keeps a unique copy of its data, for example, a struct or an enum . A reference type, shares a single copy of its data, and the type is usually a class .
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 fromSystem.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 classobject
.
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.)
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.
According to Red Gate's .NET Reflector they inherit (indirectly) from object.
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 ;)
Value types also inherit from Object
, but not directly. They inherit from ValueType
, which in turn inherits Object
.
value types inherit (indirectly) from object
... but not everything in .net inherits from object.
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.
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 struct
s 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).
int
s, for example, are value types because they are struct
s. Of course, we model int
s as value types because they represent values and we want value semantics, not reference semantics.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With