Share via


Structure inherited from Class???

Question

Thursday, March 25, 2010 4:46 PM

In C# or .NET does not allow us to inherit a class to structure then how a Structure Int32 has been inherited from a Class ValueType???

System.Object                 It's a Class
     System.ValueType      It's a Class
            System.Int32       but It's a Structure

All replies (9)

Tuesday, March 30, 2010 10:44 AM ✅Answered

By using the keyword "struct", the CLR will handle the inheritance from ValueType automatically, hence the inheritance is IMPLICIT ONLY.

I did not see your code, I just assume you are using: struct MyInt : ValueType and this will cause the compilation to fail. It's suffice to use: struct MyInt

It's a special case where struct is actually inherited from a class, and it's the ONLY class it can inherit from: ValueType. ValueType class is a "reference type" which resides on the heap, but a struct is a "value type" resides on the stack.

In the .NET Framework source code, inside ValueType.cs:

[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class ValueType {...}

For the Int32.cs:

[Serializable, System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
[System.Runtime.InteropServices.ComVisible(true)]
public struct Int32 : IComparable, IFormattable, IConvertible
#if GENERICS_WORK 
        , IComparable<Int32>, IEquatable<Int32> 
#endif
{ ...}

Friday, April 2, 2010 12:09 AM ✅Answered

Sorry I put incorrect info about special case of struct inheritance in the previous reply, I've fixed it.

The CLR defines the ValueType and Enum to be special cases in such way that they are inherited as follow this chain: System.Object (reference type)->System.ValueType(reference type)->System.Enum(reference type) where System.ValueType(reference type)->struct, int, bool...(value types) and System.Enum(reference type)->enum(value type). And inheritance in these 2 cases does not mean 'derived', rather look at the inheritance relationship as the containment relationship: System.Object contains System.ValueType, System.ValueType contains System.Enum. All value types (int, bool, struct...) except enum are contained by System.ValueType, and enum is contained by System.Enum.

Because of the speical inheritance chain defined by CLR, these value types are handled differently from reference types, that's why you don't find it anywhere about public struct Int32 : ValueType. Meanwhile, the value types can be boxed into an object because everything is contained by System.Object, hence anything in C# can be treated as an object.

Any OTHER reference types are inherited from System.Object eventually, as there can be multiple level of inheritances, and they are derived, BUT they are NOT allowed to be contained by EITHER System.ValueType OR System.Enum. That's what made the ValueType and Enum special. Certainly, value types in both ValueType and Enum cannot contain other value types, that's why value type don't have inheritance.

BaseType in the context of Int32 is just the note of inheritance, it has nothing to do about derivative, because ValueType is a special case, Int32 is not actually derived from ValueType. Difference between base type and base class can be quite different: an interface can be a base type, but it's has nothing to do with any object, since a base class must be derived from System.Object, but it does not contain any interface, therefore, they are not the same thing.

So answer your questions: No, custom class (a reference type) cannot be inherited from ValueType and a class resides in heap, but a struct is automatically inherited from ValueType and it resides in stack, that's how CLR make them work.


Thursday, March 25, 2010 7:11 PM

I'm wondering where did you get the definition of such struct cannot derive from a class.

Here's what Eric Lippert answered to the similar question: http://stackoverflow.com/questions/1682231/how-do-valuetypes-derive-from-object-referencetype-and-still-be-valuetypes


Thursday, March 25, 2010 7:19 PM

Hi, 

struct cannot inherit from another struct or class. look at the folliwing article:

http://msdn.microsoft.com/en-us/library/ah49swz4(VS.71).aspx

regards

Robert


Tuesday, March 30, 2010 1:28 AM

Hi Robert & Jack,

Sorry for for my late response.

If  you see Int32 structure in Reflector. It is showing Base Type is ValueType. ValueType is a class. How is it possible?


Tuesday, March 30, 2010 9:08 AM

Hi Dutt,

Here I'm quoting Eric Lippert's comment: All structs derive from the same class, System.ValueType, which derives from System.Object. And all enums derive from System.Enum.


Tuesday, March 30, 2010 9:30 AM

Hi Jack, Thanks for the reply

Here I'm quoting Eric Lippert's comment: All structs derive from the same class, System.ValueType, which derives from System.Object. And all enums derive from System.Enum.

No, C# wont' allow us to inherit classes to Struct. I tried, It gives compilation error. Following is the text from MSDN link

http://msdn.microsoft.com/en-us/library/ah49swz4%28VS.71%29.aspx

There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do.

Now I am wondering how the structure Int32 has been inherited from the class ValueType*?
*

I am curious to see your answer.


Thursday, April 1, 2010 3:48 PM

Thanks Jack for the reply. your are right, I am trying to create structure inherited from a class to understand Int32 structure.

struct TestStruct : TestClass
{
}

I have some queries after your reply.

I am not sure why ValueType is missing in Source Code.

Source code for Int32 copied from MSDN

[SerializableAttribute]
[ComVisibleAttribute(true)]
public struct Int32 : IComparable, IFormattable, 
    IConvertible, IComparable<int>, IEquatable<int>

If you could open Int32 in Reflector and expeds it, there is a treenode called "Base Types". It show Value Type is BaseType for Int32. I am not sure how can I understand this. is BaseType deferent from Base Class?

BTW, if reflector is showing it then already something (kind of flag) must be presented in sourcecode as reflector sees it MSIL code. So, I understood that It is not decide by CLR at runtime by seeing struct as information about basetype already written in MSIL of Int32.

It's a special case where struct is actually derived from a class, and it's the ONLY class it can derive from: ValueType. ValueType class is NOT a "reference type" which resides on the heap, it's a "value type" resides on the stack.

So, but in source code, it is not showing that Int32 extended from ValueType. What do you mean by IMPLICITY INHERITED?

ValueType class is NOT a "reference type" which resides on the heap, it's a "value type" resides on the stack.

Till now my understanding (as C++ programer) all objects of any Classes stores in heap. as per your reply. ValueType is not reference type though it was declared as class.

1. Then is there any way for us to specify a custom class as value type? (I mean creating objects of custom class in stack).
2. If YES, If we inherit from custom class from ValuTYpe then will objects of custom class be stored in stack?


Friday, April 16, 2010 6:02 AM

I'm sorry for the very late response. Your reply is very useful to me. Thanks a lot for that. I marked as answered.