Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value types inherit from System.Object...why? [duplicate]

Tags:

c#

types

object

Possible Duplicates:
Is everything in .NET an object?
How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes?

Hi, I just do not get it. System.Object is (I think) reference type but all data types in .NET inherit from it. Also value types do as well. I do not understand it - value type has its value on the stack but its inherited from Object? Hope anyone could help me to understand

like image 549
Thomas Avatar asked Jan 18 '11 21:01

Thomas


People also ask

Are value types derived from system object?

While value types are stored generally in the stack, reference types are stored in the managed heap. A value type derives from System. ValueType and contains the data inside its own memory allocation. In other words, variables or objects or value types have their own copy of the data.

Do structs inherit from object?

Yes, all struct s inherit from System. ValueType which in turn inherits from System. Object .

Is int inherited from object?

In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object. But value types(int, uiut,..) inherits from the structure System. (Value Type) e.g int derives from Struct System.

Which of the following are reference types in C#?

In C#, classes and interfaces are reference types. Variables of reference types store references to their data (objects) in memory, and they do not contain the data itself. An object of type Object , string , or dynamic is also a reference type.


2 Answers

Ask yourself why you think it's strange that a type that is typically allocated on the stack would inherit from System.Object and I think you'll find you can't formulate a good reason.

If you think it's because an object's type defines where it is allocated, you're mistaken. What member of ValueType is responsible for defining its allocation mechanism? (What member of System.Object, for that matter?)

Type inheritance in .NET is supposed to comprise "is a" relationships: a string is an object, for example. Everything in the .NET world is an object, so an int is an object, a double is an object, etc.

You can think of this in terms of the Liskov substitution principle: if I write code that expects an object, I should be capable of dealing with any type that is an object—i.e., anything at all. My code should be equally comfortable with a string, an int, a List<int>, etc.

Also note that object guarantees certain members that all types have as a consequence of this: GetType, ToString, and GetHashCode.

like image 31
Dan Tao Avatar answered Sep 28 '22 19:09

Dan Tao


You are correct that, while Object is a reference type, value types do inherit from it implicitly, as stated on msdn's reference for Value Types (according to the reference, they technically inherit from the ValueType class, which in turn inherits from Object.

C# made a special case for this so value types can benefit from Object's methods (like ToString) and properties. Also, this way, you can treat value types just like other reference types--nothing stopping you from plugging in an int inside an array of Objects.

like image 129
Zach L Avatar answered Sep 28 '22 18:09

Zach L