Atomicity of variable references 


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



ЗНАЕТЕ ЛИ ВЫ?

Atomicity of variable references



Reads and writes of the following data types are atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types. In addition, reads and writes of enum types with an underlying type in the previous list are also atomic. Reads and writes of other types, including long, ulong, double, and decimal, as well as user-defined types, are not guaranteed to be atomic. Aside from the library functions designed for that purpose, there is no guarantee of atomic read-modify-write, such as in the case of increment or decrement.


Conversions

A conversion enables an expression to be treated as being of a particular type. A conversion may cause an expression of a given type to be treated as having a different type, or it may cause an expression without a type to get a type. Conversions can be implicit or explicit, and this determines whether an explicit cast is required. For instance, the conversion from type int to type long is implicit, so expressions of type int can implicitly be treated as type long. The opposite conversion, from type long to type int, is explicit and so an explicit cast is required.

int a = 123;
long b = a; // implicit conversion from int to long
int c = (int) b; // explicit conversion from long to int

Some conversions are defined by the language. Programs may also define their own conversions (§6.4).

Implicit conversions

The following conversions are classified as implicit conversions:

· Identity conversions

· Implicit numeric conversions

· Implicit enumeration conversions.

· Implicit nullable conversions

· Null literal conversions

· Implicit reference conversions

· Boxing conversions

· Implicit dynamic conversions

· Implicit constant expression conversions

· User-defined implicit conversions

· Anonymous function conversions

· Method group conversions

Implicit conversions can occur in a variety of situations, including function member invocations (§7.5.4), cast expressions (§7.7.6), and assignments (§7.17).

The pre-defined implicit conversions always succeed and never cause exceptions to be thrown. Properly designed user-defined implicit conversions should exhibit these characteristics as well.

For the purposes of conversion, the types object and dynamic are considered equivalent.

However, dynamic conversions (§6.1.8 and §6.2.6) apply only to expressions of type dynamic (§4.7).

Identity conversion

An identity conversion converts from any type to the same type. This conversion exists such that an entity that already has a required type can be said to be convertible to that type.

Because object and dynamic are considered equivalent there is an identity conversion between object and dynamic, and between constructed types that are the same when replacing all occurences of dynamic with object.

Implicit numeric conversions

The implicit numeric conversions are:

· From sbyte to short, int, long, float, double, or decimal.

· From byte to short, ushort, int, uint, long, ulong, float, double, or decimal.

· From short to int, long, float, double, or decimal.

· From ushort to int, uint, long, ulong, float, double, or decimal.

· From int to long, float, double, or decimal.

· From uint to long, ulong, float, double, or decimal.

· From long to float, double, or decimal.

· From ulong to float, double, or decimal.

· From char to ushort, int, uint, long, ulong, float, double, or decimal.

· From float to double.

Conversions from int, uint, long, or ulong to float and from long or ulong to double may cause a loss of precision, but will never cause a loss of magnitude. The other implicit numeric conversions never lose any information.

There are no implicit conversions to the char type, so values of the other integral types do not automatically convert to the char type.

Implicit enumeration conversions

An implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any enum-type and to any nullable-type whose underlying type is an enum-type. In the latter case the conversion is evaluated by converting to the underlying enum-type and wrapping the result (§4.1.10).

Implicit nullable conversions

Predefined implicit conversions that operate on non-nullable value types can also be used with nullable forms of those types. For each of the predefined implicit identity and numeric conversions that convert from a non-nullable value type S to a non-nullable value type T, the following implicit nullable conversions exist:

· An implicit conversion from S? to T?.

· An implicit conversion from S to T?.

Evaluation of an implicit nullable conversion based on an underlying conversion from S to T proceeds as follows:

· If the nullable conversion is from S? to T?:

o If the source value is null (HasValue property is false), the result is the null value of type T?.

o Otherwise, the conversion is evaluated as an unwrapping from S? to S, followed by the underlying conversion from S to T, followed by a wrapping (§4.1.10) from T to T?.

· If the nullable conversion is from S to T?, the conversion is evaluated as the underlying conversion from S to T followed by a wrapping from T to T?.

Null literal conversions

An implicit conversion exists from the null literal to any nullable type. This conversion produces the null value (§4.1.10) of the given nullable type.

Implicit reference conversions

The implicit reference conversions are:

· From any reference-type to object and dynamic.

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

· From any class-type S to any interface-type T, provided S implements T.

· From any interface-type S to any interface-type T, provided S is 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 implicit reference conversion exists from SE to TE.

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

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

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

· From the null literal to any reference-type.

· From any reference-type to a reference-type T if it has an implicit identity or reference conversion to a reference-type T0 and T0 has an identity conversion to T.

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

· Implicit conversions involving type parameters that are known to be reference types. See §6.1.10 for more details on implicit conversions involving type parameters.

The implicit reference conversions are those conversions between reference-types that can be proven to always succeed, and therefore require no checks at run-time.

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.

Boxing conversions

A boxing conversion permits a value-type to be implicitly converted to a reference type. A boxing conversion exists from any non-nullable-value-type to object and dynamic, to System.ValueType and to any interface-type implemented by the non-nullable-value-type. Furthermore an enum-type can be converted to the type System.Enum.

A boxing conversion exists from a nullable-type to a reference type, if and only if a boxing conversion exists from the underlying non-nullable-value-type to the reference type.

A value type has a boxing conversion to an interface type I if it has a boxing conversion to an interface type I0 and I0 has an identity conversion to I.

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

Boxing a value of a non-nullable-value-type consists of allocating an object instance and copying the value-type value into that instance. A struct can be boxed to the type System.ValueType, since that is a base class for all structs (§11.3.2).

Boxing a value of a nullable-type proceeds as follows:

· If the source value is null (HasValue property is false), the result is a null reference of the target type.

· Otherwise, the result is a reference to a boxed T produced by unwrapping and boxing the source value.

Boxing conversions are described further in §4.3.1.



Поделиться:


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

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