Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

Chapter 13 Conversions

113. Conversions

2A conversion enables an expression of one type to be treated as another type. Conversions can be implicit or

3explicit, and this determines whether an explicit cast is required. [Example: For instance, the conversion

4from type int to type long is implicit, so expressions of type int can implicitly be treated as type long.

5The opposite conversion, from type long to type int, is explicit and so an explicit cast is required.

6int a = 123;

7

long b = a;

//

implicit

conversion

from

int to long

8

int c = (int) b;

//

explicit

conversion

from

long to int

9end example] Some conversions are defined by the language. Programs can also define their own

10conversions (§13.4).

1113.1 Implicit conversions

12The following conversions are classified as implicit conversions:

13Identity conversions

14Implicit numeric conversions

15Implicit enumeration conversions.

16Implicit reference conversions

17Boxing conversions

18Implicit type parameter conversions

19Implicit constant expression conversions

20User-defined implicit conversions

21Implicit conversions from an anonymous method expression to a compatible delegate type

22Implicit conversion from a method group to a compatible delegate type

23Implicit conversions can occur in a variety of situations, including function member invocations (§14.4.3),

24cast expressions (§14.6.6), and assignments (§14.13).

25The pre-defined implicit conversions always succeed and never cause exceptions to be thrown. [Note:

26Properly designed user-defined implicit conversions should exhibit these characteristics as well. end note]

2713.1.1 Identity conversion

28An identity conversion converts from any type to the same type. This conversion exists only such that an

29entity that already has a required type can be said to be convertible to that type.

3013.1.2 Implicit numeric conversions

31The implicit numeric conversions are:

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

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

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

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

131

C# LANGUAGE SPECIFICATION

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

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

3From long to float, double, or decimal.

4From ulong to float, double, or decimal.

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

6From float to double.

7Conversions from int, uint, long or ulong to float and from long or ulong to double can cause a

8loss of precision, but will never cause a loss of magnitude. The other implicit numeric conversions never

9lose any information.

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

11convert to the char type.

1213.1.3 Implicit enumeration conversions

13An implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any enum-type.

1413.1.4 Implicit reference conversions

15The implicit reference conversions are:

16From any reference-type to object.

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

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

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

20From an array-type S with an element type SE to an array-type T with an element type TE, provided all

21of the following are true:

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

23o An implicit reference conversion exists from SE to TE.

24From a one-dimensional array-type S[] to System.Collections.Generic.IList<S> and base

25interfaces of this interface.

26From a one-dimensional array-type S[] to System.Collections.Generic.IList<T> and base

27interfaces of this interface, provided there is an implicit reference conversion from S to T.

28From any array-type to System.Array.

29From any delegate-type to System.Delegate.

30From any array-type to any interface implemented by System.Array.

31From any delegate-type to System.ICloneable.

32From the null type to any reference-type.

33For a type-parameter T that is known to be a reference type (§26.7), the following implicit reference

34conversions exist:

35From T to its effective base class C, from T to any base class of C, and from T to any interface

36implemented by C.

37From T to an interface-type I in T’s effective interface set and from T to any base interface of I.

132

Chapter 13 Conversions

1From T to a type parameter U provided that T depends on U (§26.7). [Note: Since T is known to be a

2reference type, within the scope of T, the run-time type of U will always be a reference type, even if U is

3not known to be a reference type at compile-time. end note]

4From the null type to T.

5The implicit reference conversions are those conversions between reference-types that can be proven to

6always succeed, and therefore require no checks at run-time.

7Reference conversions, implicit or explicit, never change the referential identity of the object being

8converted. [Note: In other words, while a reference conversion can change the type of the reference, it never

9changes the type or value of the object being referred to. end note]

1013.1.5 Boxing conversions

11A boxing conversion permits any value-type to be implicitly converted to the type object or

12System.ValueType or to any interface-type implemented by the value-type, and any enum type to be

13implicitly converted to System.Enum as well. Boxing a value of a value-type consists of allocating an

14object instance and copying the value-type value into that instance. An enum can be boxed to the type

15System.Enum, since that is the direct base class for all enums (§21.4). A struct or enum can be boxed to the

16type System.ValueType, since that is the direct base class for all structs (§18.3.2) and a base class for all

17enums.

18For a type-parameter T that is not known to be a reference type (§26.7), the following conversions involving

19T are considered to be boxing conversions at compile-time. At run-time, if T is a value type, the conversion

20is executed as a boxing conversion. At run-time, if T is a reference type, the conversion is executed as an

21implicit reference conversion or identity conversion.

22From T to its effective base class C, from T to any base class of C, and from T to any interface

23implemented by C. [Note: C will be one of the types System.Object, System.ValueType, or

24System.Enum (otherwise T would be known to be a reference type and §13.1.4 would apply instead of

25this clause). end note]

26From T to an interface-type I in T’s effective interface set and from T to any base interface of I.

27Boxing conversions are described further in §11.3.1.

2813.1.6 Implicit type parameter conversions

29This clause details implicit conversions involving type parameters that are not classified as implicit

30reference conversions or implicit boxing conversions.

31For a type-parameter T that is not known to be a reference type, there is an implicit conversion from T to a

32type parameter U provided T depends on U. At run-time, if T is a value type and U is a reference type, the

33conversion is executed as a boxing conversion. At run-time, if both T and U are value types, then T and U are

34necessarily the same type and no conversion is performed. At run-time, if T is a reference type, then U is

35necessarily also a reference type and the conversion is executed as an implicit reference conversion or

36identity conversion (§26.7).

3713.1.7 Implicit constant expression conversions

38An implicit constant expression conversion permits the following conversions:

39A constant-expression (§14.15) of type int can be converted to type sbyte, byte, short, ushort,

40uint, or ulong, provided the value of the constant-expression is within the range of the destination

41type.

42A constant-expression of type long can be converted to type ulong, provided the value of the constant-

43expression is not negative.

133

Соседние файлы в предмете Электротехника