Мы поможем в написании ваших работ!



ЗНАЕТЕ ЛИ ВЫ?

Explicit reference conversions

Поиск

The explicit reference conversions are:

· From object and dynamic to any other reference-type.

· From any class-type S to any class-type T, provided S is a base class of T.

· From any class-type S to any interface-type T, provided S is not sealed and provided S does not implement T.

· From any interface-type S to any class-type T, provided T is not sealed or provided T implements S.

· From any interface-type S to any interface-type T, provided S is not derived from T.

· From an array-type S with an element type SE to an array-type T with an element type TE, provided all of the following are true:

o S and T differ only in element type. In other words, S and T have the same number of dimensions.

o Both SE and TE are reference-types.

o An explicit reference conversion exists from SE to TE.

· From System.Array and the interfaces it implements to any array-type.

· From a single-dimensional array type S[] to System.Collections.Generic.IList<T> and its base interfaces, provided that there is an explicit reference conversion from S to T.

· From System.Collections.Generic.IList<S> and its base interfaces to a single-dimensional array type T[], provided that there is an explicit identity or reference conversion from S to T.

· From System.Delegate and the interfaces it implements to any delegate-type.

· From a reference type to a reference type T if it has an explicit reference conversion to a reference type T0 and T0 has an identity conversion T.

· From a reference type to an interface or delegate type T if it has an explicit reference conversion to an interface or delegate type T0 and either T0 is variance-convertible to T or T is variance-convertible to T0 (§13.1.3.2).

· From D<S1…Sn> to D<T1…Tn> where D<X1…Xn> is a generic delegate type, D<S1…Sn> is not compatible with or identical to D<T1…Tn>, and for each type parameter Xi of D the following holds:

o If Xi is invariant, then Si is identical to Ti.

o If Xi is covariant, then there is an implicit or explicit identity or reference conversion from Si to Ti.

o If Xi is contravariant, then Si and Ti are either identical or both reference types.

· Explicit conversions involving type parameters that are known to be reference types. For more details on explicit conversions involving type parameters, see §6.2.7.

The explicit reference conversions are those conversions between reference-types that require run-time checks to ensure they are correct.

For an explicit reference conversion to succeed at run-time, the value of the source operand must be null, or the actual type of the object referenced by the source operand must be a type that can be converted to the destination type by an implicit reference conversion (§6.1.6) or boxing conversion (§6.1.7). If an explicit reference conversion fails, a System.InvalidCastException is thrown.

Reference conversions, implicit or explicit, never change the referential identity of the object being converted. In other words, while a reference conversion may change the type of the reference, it never changes the type or value of the object being referred to.

Unboxing conversions

An unboxing conversion permits a reference type to be explicitly converted to a value-type. An unboxing conversion exists from the types object, dynamic and System.ValueType to any non-nullable-value-type, and from any interface-type to any non-nullable-value-type that implements the interface-type. Furthermore type System.Enum can be unboxed to any enum-type.

An unboxing conversion exists from a reference type to a nullable-type if an unboxing conversion exists from the reference type to the underlying non-nullable-value-type of the nullable-type.

A value type S has an unboxing conversion from an interface type I if it has an unboxing conversion from an interface type I0 and I0 has an identity conversion to I.

A value type S has an unboxing conversion from an interface type I if it has an unboxing conversion from an interface or delegate type I0 and either I0 is variance-convertible to I or I is variance-convertible to I0 (§13.1.3.2).

An unboxing operation consists of first checking that the object instance is a boxed value of the given value-type, and then copying the value out of the instance. Unboxing a null reference to a nullable-type produces the null value of the nullable-type. A struct can be unboxed from the type System.ValueType, since that is a base class for all structs (§11.3.2).

Unboxing conversions are described further in §4.3.2.

Explicit dynamic conversions

An explicit dynamic conversion exists from an expression of type dynamic to any type T. The conversion is dynamically bound (§7.2.2), which means that an explicit conversion will be sought at run-time from the run-time type of the expression to T. If no conversion is found, a run-time exception is thrown.

If dynamic binding of the conversion is not desired, the expression can be first converted to object, and then to the desired type.

Assume the following class is defined:

class C
{
int i;

public C(int i) { this.i = i; }

public static explicit operator C(string s)
{
return new C(int.Parse(s));
}
}

The following example illustrates explicit dynamic conversions:

object o = "1";
dynamic d = "2";

var c1 = (C)o; // Compiles, but explicit reference conversion fails
var c2 = (C)d; // Compiles and user defined conversion succeeds

The best conversion of o to C is found at compile-time to be an explicit reference conversion. This fails at run-time, because “1” is not in fact a C. The conversion of d to C however, as an explicit dynamic conversion, is suspended to run-time, where a user defined conversion from the run-time type of d – string – to C is found, and succeeds.

Explicit conversions involving type parameters

The following explicit conversions exist for a given type parameter T:

· From the effective base class C of T to T and from any base class of C to T. At run-time, if T is a value type, the conversion is executed as an unboxing conversion. Otherwise, the conversion is executed as an explicit reference conversion or identity conversion.

· From any interface type to T. At run-time, if T is a value type, the conversion is executed as an unboxing conversion. Otherwise, the conversion is executed as an explicit reference conversion or identity conversion.

· From T to any interface-type I provided there is not already an implicit conversion from T to I. At run-time, if T is a value type, the conversion is executed as a boxing conversion followed by an explicit reference conversion. Otherwise, the conversion is executed as an explicit reference conversion or identity conversion.

· From a type parameter U to T, provided T depends on U (§10.1.5). At run-time, if U is a value type, then T and U are necessarily the same type and no conversion is performed. Otherwise, if T is a value type, the conversion is executed as an unboxing conversion. Otherwise, the conversion is executed as an explicit reference conversion or identity conversion.

If T is known to be a reference type, the conversions above are all classified as explicit reference conversions (§6.2.4). If T is not known to be a reference type, the conversions above are classified as unboxing conversions (§6.2.5).

The above rules do not permit a direct explicit conversion from an unconstrained type parameter to a non-interface type, which might be surprising. The reason for this rule is to prevent confusion and make the semantics of such conversions clear. For example, consider the following declaration:

class X<T>
{
public static long F(T t) {
return (long)t; // Error
}
}

If the direct explicit conversion of t to int were permitted, one might easily expect that X<int>.F(7) would return 7L. However, it would not, because the standard numeric conversions are only considered when the types are known to be numeric at binding-time. In order to make the semantics clear, the above example must instead be written:

class X<T>
{
public static long F(T t) {
return (long)(object)t; // Ok, but will only work when T is long
}
}

This code will now compile but executing X<int>.F(7) would then throw an exception at run-time, since a boxed int cannot be converted directly to a long.



Поделиться:


Последнее изменение этой страницы: 2016-08-10; просмотров: 281; Нарушение авторского права страницы; Мы поможем в написании вашей работы!

infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 3.145.103.169 (0.005 с.)