Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Unboxing conversions

The object type | Boxing conversions | Satisfying constraints | Expression tree types | Local variables | Definite assignment | Simple assignment expressions | Anonymous functions | Implicit conversions involving type parameters | Explicit numeric conversions |


Читайте также:
  1. Anonymous function conversions
  2. Boxing and unboxing
  3. Boxing conversions
  4. Evaluation of user-defined conversions
  5. Explicit numeric conversions
  6. Explicit reference conversions
  7. Good Things Come from Pagan 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.


Дата добавления: 2015-11-16; просмотров: 53 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Explicit reference conversions| Evaluation of user-defined conversions

mybiblioteka.su - 2015-2024 год. (0.006 сек.)