32

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.

Michał Kuliński
  • 1,928
  • 3
  • 27
  • 51
P.K
  • 18,587
  • 11
  • 45
  • 51

4 Answers4

37

Eric Lippert has covered this in a blog entry: Not everything derives from object (This is the title of the blog entry; 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.)

kdbanman
  • 10,161
  • 10
  • 46
  • 78
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • And I just linked that entry in another post ... – Brian Rasmussen Aug 22 '09 at 18:16
  • great link...but this has confused me more. Eric says 'All value types, including enums and nullable types, derive from object...' – P.K Aug 22 '09 at 18:22
  • Enums and structs inherit from `System.ValueType` which in turn inherits from `System.Object`. – Mehrdad Afshari Aug 22 '09 at 18:23
  • @Mehrdad You say Enums and structs inherit from System.ValueType. Where can I see this ? I can't see this using reflector. – P.K Aug 22 '09 at 18:33
  • 1
    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 Aug 22 '09 at 18:35
  • 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 Aug 22 '09 at 18:44
  • @PK: You can see it using reflector if you swith to IL. – Alfred Myers Aug 22 '09 at 19:00
  • @Mehrdad '...and structs you write in code are a structs'. What does this mean? – P.K Aug 22 '09 at 19:20
  • PK: Comment formatting screwed that up. I'll repost it. – Mehrdad Afshari Aug 22 '09 at 19:28
  • 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 Aug 22 '09 at 19:31
8

Not every type, but System.Double does.

From Eric Lippert's post:

All value types, including enums and nullable types, derive from object

In C# struct is syntatic sugar for System.ValueType, meaning that System.Double derives from System.ValueType. Since System.ValueType derives from System.Object, System.Double does too. You can see that using the .Net Framework source code or using .Net Reflector:


.class public sequential ansi serializable sealed beforefieldinit Double
    extends System.ValueType


UPDATE:

On the toolbar, if you select C#, you'll see (This is what you are seeing): alt text http://bqqqkg.bay.livefilestore.com/y1pk3EAm_SJtl4dn51HLrhdgHAXCS08-xY9nQUARCpT4WnBQyRHD4RLmIK9zZ4okJXMj7Xopg1EBuY_Od7_oWz7Pw/DoubleCSharp.jpg

If you change the ComboBox and select IL then you'll see: alt text http://bqqqkg.bay.livefilestore.com/y1pRXk-0rDvFjj7b8EqU9-bydzpWjVGJMq8pDbiCr6aALob3j-aC9vvbeBS4vQRedHJ5Dh2CWtYRCywMJ9FGHOaaw/DoubleIL.jpg

See that in IL, Double extends System.ValueType

Alfred Myers
  • 6,384
  • 1
  • 40
  • 68
  • Its not just Double, it is all the basic types such as int, short, long, decimal etc - that derive from System.ValueType. – shahkalpesh Aug 22 '09 at 18:30
  • 1
    I do know that. I happend to use Double as an example he did. – Alfred Myers Aug 22 '09 at 18:34
  • 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 Aug 22 '09 at 18:35
  • 5
    @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 Aug 22 '09 at 18:44
4

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

Community
  • 1
  • 1
Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
3

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
  • 33,172
  • 3
  • 63
  • 88