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!