C# doesn't allow structs to derive from classes, but all ValueTypes derive from Object. Where is this distinction made?
How does the CLR handle this?
C# doesn't allow structs to derive from classes, but all ValueTypes derive from Object. Where is this distinction made?
How does the CLR handle this?
This is a somewhat artificial construct maintained by the CLR in order to allow all types to be treated as a System.Object.
Value types derive from System.Object through System.ValueType, which is where the special handling occurs (ie: the CLR handles boxing/unboxing, etc for any type deriving from ValueType).
Small correction, C# doesn't allow structs to custom derive from anything, not just classes. All a struct can do is implement an interface which is very different from derivation.
I think the best way to answer this is that ValueType
is special. It is essentially the base class for all value types in the CLR type system. It's hard to know how to answer "how does the CLR handles this" because it's simply a rule of the CLR.
C# doesn't allow structs to derive from classes
Your statement is incorrect, hence your confusion. C# does allow structs to derive from classes. All structs derive from the same class, System.ValueType, which derives from System.Object. And all enums derive from System.Enum.
Where is this distinction made?
It's made everywhere in the world that the compiler runs. :-)
Seriously, I don't understand the question. What do you mean by "where"?
How does the CLR handle this?
Extremely well. :-)
Again, I don't understand what you're asking. What makes a value type a value type is that its instances are copied by value. What makes a reference type a reference type is that its instances are copied by reference. You seem to have some belief that the inheritance relationship between value types and reference types is somehow special and unusual, but I don't understand what that belief is. Inheritance has nothing to do with how things are copied.
Look at it this way. Suppose I told you the following facts:
There are two kinds of boxes, red boxes and blue boxes.
Every red box is empty.
There are three special blue boxes called O, V and E.
O is not inside any box.
V is inside O.
E is inside V.
No other blue box is inside V.
No blue box is inside E.
Every red box is in either V or E.
Every blue box other than O is itself inside a blue box.
The blue boxes are reference types, the red boxes are value types, O is System.Object, V is System.ValueType, E is System.Enum, and the "inside" relationship is "derives from".
That's a perfectly consistent and straightforward set of rules which you could easily implement yourself, if you had a lot of cardboard and a lot of patience. Whether a box is red or blue has nothing to do with what it's inside; in the real world it is perfectly possible to put a red box inside a blue box. In the CLR, it is perfectly legal to make a value type that inherits from a reference type, so long as it is either System.ValueType or System.Enum.
So let's rephrase your question:
How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes?
as
How is it possible that every red box (value types) is inside (derives from) box O (System.Object), which is a blue box (a reference Type) and still be a red box (a value type)?
When you phrase it like that, I hope it's obvious. There's nothing stopping you from putting a red box inside box V, which is inside box O, which is blue. Why would there be?