What makes ValueType class Special?

2019-02-09 02:39发布

问题:

When I trying to define a class which inherits from System.ValueType or System.Enum class, I'm getting an error:

Cannot derive from special class System.ValueType

I understand that error but what I couldn't understand is what makes ValueType class special? I mean there is no keyword (like sealed) or attribute to specify that this class can not be inherited.ValueType has two attributes, Serializable and ComVisible but none of them is relevant with that case.The documentation says:

Although ValueType is the implicit base class for value types, you cannot create a class that inherits from ValueType directly. Instead, individual compilers provide a language keyword or construct (such as struct in C# and Structure…End Structure in Visual Basic) to support the creation of value types.

But it doesn't answer my question.So my question is how the compiler is informed in this case? Does the compiler directly check whether the class is ValueType or Enum when I try to create a class that inherit from a class?

Edit: Also all structs implicitly inherit from ValueType, but Enum class Explicitly inherit from ValueType,so how is that working? How the compiler figure out this situation, all of this are hard coded by compiler?

回答1:

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?



回答2:

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



回答3:

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 from System.Object. ... In the .NET Framework class library, all primitive data types (Boolean, Byte,Char, DateTime, Decimal, Double, Int16, Int32, Int64, SByte, Single, UInt16, UInt32, and UInt64) 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!