When I trying to define a class which inherits from System.ValueType
or System.Enum
class, I\'m getting an error:
Cannot derive fro
System.ValueType is a special handled class for the compiler, used to annotate the value types. It is used differently by the compiler, because value type objects are handled differently than reference type objects. I suppose this series of blog posts could provide some clarification about the differences betweebn value and reference types. This MSDN post describes the common cases of value reference types so that you could categorize each type easily.
The answer to your question, is in the .NET Common Type System. If you want to create your own value type class, I would suggest to create a structure. Copying from (Common TYpe System, Structures reference](http://msdn.microsoft.com/en-us/library/zcx1eb1e%28v=vs.110%29.aspx#Structures):
A structure is a value type that derives implicitly from
System.ValueType
, which in turn is derived fromSystem.Object
. ... In the .NET Framework class library, all primitive data types (Boolean
,Byte,
Char,DateTime
,Decimal
,Double
,Int16
,Int32
,Int64
,SByte
,Single
,UInt16
,UInt32
, andUInt64
) are defined as structures.Like classes, structures define both data (the fields of the structure) and the operations that can be performed on that data (the methods of the structure). ...
Value types also differ from classes in several respects. First, although they implicitly inherit from
System.ValueType
, they cannot directly inherit from any type. Similarly, all value types are sealed, which means that no other type can be derived from them. ...For each value type, the common language runtime supplies a corresponding boxed type, which is a class that has the same state and behavior as the value type. ... When you define a value type, you are defining both the boxed and the unboxed type.
Hope I helped!
Microsoft does not publish its C# compiler source code, so we can only guess the check is embedded at compiler level.
Mono's C# compiler performs this kind of check at compile time, which you can see around line 2790 in Class.ResolveBaseTypes
method,
https://github.com/mono/mono/blob/master/mcs/mcs/class.cs
I understand that error but what I couldn't understand is what makes ValueType class special?
The class is documented as being special. That's what makes it special.
how the compiler is informed in this case?
The compiler writers read the documentation before they write the compiler.
Does the compiler directly check whether the class is ValueType or Enum when I try to create a class that inherit from a class?
Yes.
Also all structs implicitly inherit from ValueType, but Enum class Explicitly inherit from ValueType, so how is that working?
It's working pretty well.
Are all of these special cases hard coded into the compiler?
Yes.
Isn't it more appropriate to create an attribute to specify that this class is special and cannot be inherited instead of hard coding?
No, it's not. That would imply that a third party could also make a special type that needed special handling by the compiler when inherited from. How would the third party then modify the compiler to implement those rules?