none
Can a structure inherit from class?

    Question

  • Hi,
    In Dot net, we can not inherit a struct (value type) from any thing. it can just implement an interface.
    Then how come by default a struct inherits from System.ValueType?
    Isnt itbreaking the irony of OOP?

    Aslo how can a built in value types have methods of Object class?

    Thanking you..
    Monday, October 20, 2008 1:36 PM

Answers

  • ValueType manages to inherit from Object through a special operation called "boxing".  Calling that inheritance is a bit of a mental stretch but otherwise works similar.  The type of such a boxed value is ValueType.  It is a useful abstract type to provide more appropriate versions of Equals() and GetHashCode().  The versions provided by Object are not suitable for value types.  Since any concrete value type can be boxed (i.e. converted to ValueType) you can make a similar mental stretch that value types inherit from ValueType.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Friday, October 24, 2008 11:23 AM
    Tuesday, October 21, 2008 3:02 AM

All replies

  • ValueType is a special class that overrides the base methods of Object to have methods that are more suitable for value types.  It's a special class, and is compiled specially with .NET compilers.  It doesn't break OOP because all value types inherit from this.  Becuase a struct inherits from System.ValueType, and because .NET doesn't support multiple inheritance, this is the reason you can't inherit a struct from a class.  Besides, the behavior of each would be vastly different, as a base class would be a reference type, while the derived item is a value type.  How would the memory be stored?  As a class, or as a value?.  ValueType itself is a class, but it is compiled differently, as it's a "special" class.

    Because ValueType inherits from Object, it overrides the implementations of those methods, which is why value types have the methods of the object class.
    David Morton - http://blog.davemorton.net/
    Monday, October 20, 2008 1:46 PM
  •  Thanks bro, 
        If the Valuetype class is a special class, it solves all the confusion i had. Thank you once again.

    But again, as  u said, dotnet dose not have multiple inheritance, but each class inherit from System.Object, but even then, it can inherit from one normal class. Is Object class also a spacial class?
    Monday, October 20, 2008 1:53 PM
  • Objects that inherit from other objects do not inherit from object directly, but rather from the inherited object.

    So let's say you have these two classes:

    public class BaseClass { }  
     
    public class DerivedClass : BaseClass { } 


    DerivedClass doesn't inherit directly from Object in this instance, but it inherits from BaseClass, which itself inherits directly from Object.  It's not multiple inheritance, it's levels of inheritance in this situation.  Ultimately, DerivedClass inherits from object, but only through BaseClass in this situation.
    David Morton - http://blog.davemorton.net/
    Monday, October 20, 2008 2:10 PM
  • ValueType manages to inherit from Object through a special operation called "boxing".  Calling that inheritance is a bit of a mental stretch but otherwise works similar.  The type of such a boxed value is ValueType.  It is a useful abstract type to provide more appropriate versions of Equals() and GetHashCode().  The versions provided by Object are not suitable for value types.  Since any concrete value type can be boxed (i.e. converted to ValueType) you can make a similar mental stretch that value types inherit from ValueType.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Friday, October 24, 2008 11:23 AM
    Tuesday, October 21, 2008 3:02 AM