I know that all value types are derived implicitly from the System.ValueType. and structs can implement interfaces, but I need to know why cannot derive from value types in C#.
I know that all value types are derived implicitly from the System.ValueType. and structs
Share
Firstly, value-types have no object header (because they aren’t objects), so there would be no way to identify the actual type, or to do virtual dispatch.
Secondly – how could you add fields to sub types? The size has to be known by the compiler (for stack space etc), so:
must always take the same amount of space.
Likewise, an abstract base-type wouldn’t work, as you can always construct a struct.
Basically, they would be horrible malformed things, crippled and ugly.
I find it interesting that you would want a subtype of a value – that sounds a bit like a confused usage of a struct.