tags:

views:

665

answers:

4

This might be a very basic question. But, I am a bit confused about it. If I reflect the Int32/Double/any value type code, I see that they are structs and look like :

[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)]
public struct Double : IComparable, IFormattable, IConvertible, IComparable<double>, IEquatable<double>
{
....
}

So, why do we say that everything in .net is derived from System.Object. I think am missing some crucial point here.

EDIT: What confuses me further is that how can a value type which is struct inherit from System.Object which is a class.

+16  A: 

Eric Lippert has covered this in a blog entry: Not everything derives from object (This is the title of the post; not the answer to this question. Don't get confused.)

Yes, all structs inherit from System.ValueType which in turn inherits from System.Object. enums you declare inherit from System.Enum which inherits from System.ValueType.

Update:

Inherently, there's not a problem with a value type being derived from a reference type. Inheritance is a "is-a" relationship between two types. However, in order to treat a value type as an object instance, it has to be boxed. This is done implicitly when you are passing a value to a method that expects an object parameter (or when you call instance methods implemented in System.Object.)

Mehrdad Afshari
Beat me by 3 seconds :)
hmemcpy
And I just linked that entry in another post ...
Brian Rasmussen
great link...but this has confused me more. Eric says 'All value types, including enums and nullable types, derive from object...'
P.K
Enums and structs inherit from `System.ValueType` which in turn inherits from `System.Object`.
Mehrdad Afshari
@Mehrdad You say Enums and structs inherit from System.ValueType. Where can I see this ? I can't see this using reflector.
P.K
PK: You can see using .NET Reflector and `ildasm.exe`. In Reflector, go to mscorlib > System > Int32, Enum, ... > Base Types and you'll see System.ValueType.
Mehrdad Afshari
Now this means that every type in .net is in fact a class. My head is now spinning.Also, I can see Enum, but not struct using the reflector.
P.K
@PK: You can see it using reflector if you swith to IL.
Alfred Myers
@Mehrdad '...and structs you write in code are a structs'. What does this mean?
P.K
PK: Comment formatting screwed that up. I'll repost it.
Mehrdad Afshari
PK: `struct` itself is not a type. For instance, Int32, Double, and structs you write in code are types that directly from System.ValueType. Each enums you declare is also a separate type that inherits from `System.Enum` which inherits from System.ValueType.
Mehrdad Afshari
+4  A: 

Just few weeks ago Eric Lippert blogged about this: Not everything derives from object. A great read.

hmemcpy
+1  A: 

As far as your question is concerned, structs inherit from ValueType implicitly, which in turn derives from object.

Other than that, Eric Lippert's link posted above is what you would like to read.

shahkalpesh
+4  A: 
Alfred Myers
Its not just Double, it is all the basic types such as int, short, long, decimal etc - that derive from System.ValueType.
shahkalpesh
I do know that. I happend to use Double as an example he did.
Alfred Myers
Double is a struct. How can it derive from System.ValueType. Also, I can see the code of Double. It just implements interfaces.
P.K
@PK: Please read the answer. If you look at the IL instead of metadata or the code decompiled into C#, you'll see that Double inherits from ValueType. From the C# spec: Structs are value types ($11.3.1)All struct types implicitly inherit from the class System.ValueType ($11.3.2).
Alfred Myers