[Translation] Not all types inherit from object

Source: Internet
Author: User

[Original text fromEric lippertOf:Not everything derives from object]
I have heard many myths about C. These myths are usually true. For example, "value types are always distributed on stacks ". If you replace "always" with "sometimes", then some incorrect mythical Arguments become true.

I often hear that "in C #, all types are inherited from objects" (in C # Every type derives from object ). This is not correct!

First, the pointer type does not inherit from the object and cannot be converted to the object. Insecure pointer types are obviously out of the conventional type rules in this language. (If You Want To regard the pointer as a value type, you can convert it to system. intptr and then use it as a value type .) Let's leave the pointer type aside.

Many types are inherited from objects. All value types, including enumeration and Null Types, inherit from object. All classes, arrays, and delegate types are inherited from objects.

Here are all "specific" types. Any object instance you encounter is null, or a class, Delegate, array, structure, enumeration, or null type. So here we need to correct it. Although the difference is not big, we should say that all instances inherit from objects. But that is not a myth. That myth says that all types are inherited from objects.

The interface type, not a class, does not inherit from the object. They can all be converted to objects, of course, because we know that the instance is of a specific type at runtime. However, an interface only inherits from other interface types. An object is not an interface type.

The "open" type parameter (: Generic Type parameter) type is not inherited from the object. Of course they are type. You can create a field of the type parameter. When generic classes and methods are "constructed" only by "specific" type parameters at runtime, the type parameters can always be converted to objects. (This is why you cannot create ienumerable <void *> ---- we stipulate that type parameters must be converted to objects .) Type parameter types do not inherit from anything; they have a "valid base class", so type parameters are constrained to inherit from valid base classes, but they are not "inherited from" anything.

To correct this myth, you can simply replace "inherited from" (derives from) with "convertible to" (is convertible to) and ignore the pointer type: in C #, any non-pointer type can be converted to an object. (Every non-pointer type in C # is convertible to object .)

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.