I am confused about inheritance in .NET and here is why.
I was previously of the understanding that a structure cannot inherit from a class in .NET, so for example, the following won’t compile:
struct MyStruct : MyClass
{
}
But today I read that integers (and other value types) are structs and not objects and they inherit from the ValueType class. The ValueType class inherits from System.Object and its purpose is to override certain methods in System.Object to make these methods suitable for Value Types.
So what’s the deal? Can a structure inherit from a class in .NET, can it not or can it only in certain circumstances?
Thanks
Within the guts of .net, the definition for a struct containing certain members is the same as a definition for a class which those same fields and members, and which inherits from
System.ValueType. Note that compilers will not allow one to declare aclasswhich inheritsValueType, but when one declares astruct, the compiler, “behind the scenes” declares a class which does.What makes value types special in .net is the way the run-time allocates storage locations (variables, fields, parameters, etc.) When a storage location of a type not inheriting from
ValueTypeis declared, the runtime will allocate space for a heap object reference. By contrast, when a storage location of a type inheriting fromValueTypeis declared, the runtime will allocate space for all the public and private fields of that type. For a type likeint, the system allocates a private field which is of a special primitive type, outside the normal type system.Note that a storage location of a value type doesn’t really hold an instance of that type; instead is an instance of that type, and holds all of the fields of that type. A statement like
struct1 = struct2does not replace the value-type instancestruct1with the instancestruct2. Instead, it copies all of the fields fromstruct2over the corresponding fields instruct1. Likewise if a value-type storage location is passed as a method to a procedure without using therefkeyword, what is passed is not the struct instance itself, but rather the contents of its fields.If it is necessary to copy a value-type storage location to a one of type not derived from
ValueType(e.g.ObjectorIComparable), the system will create a new heap-object instance of the value type, copy all the fields from the value type to that new instancen and store a reference to that new instance in the target storage location. This process is called “boxing”. Most compilers will do this implicitly, thus attempting to behave as though a value type storage location holds an object which derives fromValueType. It’s important to note, though, that this is an illusion. If typeXderives fromY, one has anXnamedxxand aYnamedyy, and one performsxx = yy, such a statement should causexxandyyto refer to the same object instance. That will happen ifxxandyyare types not derived fromValueType, even ifyyholds an instance of something derived fromValueType. It will not happen, however, ifxxand/oryyderives fromValueType. In that case, the system will copy fields from one instance to another (possibly new) instance.