All value types inherit from System.ValueType's doubts

Source: Internet
Author: User
Tags inheritance

For example int bar, is the value type, is a struct, is this declares

public struct int32:icomparable, IFormattable, IConvertible, IComparable, iequatable

How can you see to inherit from ValueType?? Besides, struct not inherit? Why do you inherit from valuetype?? Puzzled....

Reply:

ValueType is indeed a class type, but his subclass is a value type because the CLR has a special treatment for his subclasses. If the CLR determines that a class inherits from ValueType, it has a characteristic of a value type in memory allocation (for example, directly on the stack). This has little to do with the inheritance hierarchy of the class library.

The compiler on this side of C # has also been specially treated so that value types differ in many ways from class types, for example, we can not declare a value type that inherits from ValueType, and so on.

Simply put, Microsoft has done special processing of value types in. NET to inherit from valuetype. Note: syntax struct is not allowed to inherit, but can implement interfaces. I think Microsoft is disgusting. I always do something that doesn't match with grammar.

Many types do not show inherited base classes,. NET defaults to inherit object, and in string, for example, inherits only the object directly.

struct is directly inherited ValueType, the default, but struct has a feature that can not show the inheritance base class, that is, can only inherit valuetype, so that's it. So it looks like the landlord.

The book says can't inherit base class, because C # is a single inheritance, already have this valuetype, so can't inherit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.