Explicit reference conversions for C #

Source: Internet
Author: User

An explicit reference conversion includes:

From object to any reference type

From the class type S to the class type T, where S is the base class for T.

From the base type S to the interface type T, where S is not a sealed class and does not implement T.

From the interface type S to the class type T, where T is not a sealed class, and does not implement S.

From the interface type S to the interface type T, where S is not a sub-interface of T.

The conversion of an array type S with an element type of TS to a type T with an element type of TT requires that the following conditions be met:

S and T are only the data types of the elements, but the dimensions are the same.

Both TS and TT are reference types.

There is an explicit reference conversion from TS to TT.

From System.Array to array type.

From System.Delegate to Representative type.

From system.icloneable to array type or representative type.

Explicit reference conversions occur between reference types and need to be detected at run time to ensure that they are correct.

To ensure the normal execution of an explicit reference conversion, the value of the source variable must be null or the type of the object it refers to can be converted to the target type by an implicit reference. Otherwise, an explicit reference conversion fails, and a InvalidCastException exception is thrown.

Implicit or explicit reference conversions, although they may change the type of reference value, do not change the value itself.

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.