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

C# LANGUAGE SPECIFICATION

113.1.8 User-defined implicit conversions

2A user-defined implicit conversion consists of an optional standard implicit conversion, followed by

3execution of a user-defined implicit conversion operator, followed by another optional standard implicit

4conversion. The exact rules for evaluating user-defined conversions are described in §13.4.3.

513.2 Explicit conversions

6The following conversions are classified as explicit conversions:

7All implicit conversions.

8Explicit numeric conversions.

9Explicit enumeration conversions.

10Explicit reference conversions.

11Explicit interface conversions.

12Unboxing conversions.

13Explicit type parameter conversions.

14User-defined explicit conversions.

15Explicit conversions can occur in cast expressions (§14.6.6).

16The set of explicit conversions includes all implicit conversions. [Note: This means that redundant cast

17expressions are allowed. end note]

18The explicit conversions that are not implicit conversions are conversions that cannot be proven to always

19succeed, conversions that are known to possibly lose information, and conversions across domains of types

20sufficiently different to merit explicit notation.

2113.2.1 Explicit numeric conversions

22The explicit numeric conversions are the conversions from a numeric-type to another numeric-type for

23which an implicit numeric conversion (§13.1.2) does not already exist:

24From sbyte to byte, ushort, uint, ulong, or char.

25From byte to sbyte or char.

26From short to sbyte, byte, ushort, uint, ulong, or char.

27From ushort to sbyte, byte, short, or char.

28From int to sbyte, byte, short, ushort, uint, ulong, or char.

29From uint to sbyte, byte, short, ushort, int, or char.

30From long to sbyte, byte, short, ushort, int, uint, ulong, or char.

31From ulong to sbyte, byte, short, ushort, int, uint, long, or char.

32From char to sbyte, byte, or short.

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

34From double to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or decimal.

35From decimal to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double.

36Because the explicit conversions include all implicit and explicit numeric conversions, it is always possible

37to convert from any numeric-type to any other numeric-type using a cast expression (§14.6.6).

134

Chapter 13 Conversions

1The explicit numeric conversions possibly lose information or possibly cause exceptions to be thrown. An

2explicit numeric conversion is processed as follows:

3For a conversion from an integral type to another integral type, the processing depends on the overflow

4checking context (§14.5.12) in which the conversion takes place:

5o In a checked context, the conversion succeeds if the value of the source operand is within the range

6of the destination type, but throws a System.OverflowException if the value of the source

7operand is outside the range of the destination type.

8o In an unchecked context, the conversion always succeeds, and proceeds as follows.

9If the source type is larger than the destination type, then the source value is truncated by

10

11

12

13

14

15

16

17

discarding its “extra” most significant bits. The result is then treated as a value of the destination type.

If the source type is smaller than the destination type, then the source value is either signextended or zero-extended so that it is the same size as the destination type. Sign-extension is used if the source type is signed; zero-extension is used if the source type is unsigned. The result is then treated as a value of the destination type.

If the source type is the same size as the destination type, then the source value is treated as a value of the destination type

18For a conversion from decimal to an integral type, the source value is rounded towards zero to the

19nearest integral value, and this integral value becomes the result of the conversion. If the resulting

20integral value is outside the range of the destination type, a System.OverflowException is thrown.

21For a conversion from float or double to an integral type, the processing depends on the overflow-

22checking context (§14.5.12) in which the conversion takes place:

23o In a checked context, the conversion proceeds as follows:

24The value is rounded towards zero to the nearest integral value. If this integral value is within

25

the range of the destination type, then this value is the result of the conversion.

26Otherwise, a System.OverflowException is thrown.

27o In an unchecked context, the conversion always succeeds, and proceeds as follows.

28The value is rounded towards zero to the nearest integral value. If this integral value is within

29

the range of the destination type, then this value is the result of the conversion.

30Otherwise, the result of the conversion is an unspecified value of the destination type.

31For a conversion from double to float, the double value is rounded to the nearest float value. This

32rounding may cause a non-zero value to be rounded to a zero value of the same sign. If the magnitude of

33the double value is too large to represent as a float, the result becomes positive infinity or negative

34infinity. If the double value is NaN, the result is also NaN.

35For a conversion from float or double to decimal, the source value is converted to decimal

36representation and rounded to the nearest number after the 28th decimal place if required (§11.1.7). This

37rounding may cause a non-zero value to be rounded to zero. If the source value is NaN, infinite, or its

38magnitude is too large to represent as a decimal, a System.OverflowException is thrown.

39For a conversion from decimal to float or double, the decimal value is rounded to the nearest

40double or float value. However, if the value being converted is not within the range of the destination

41type, a System.OverflowException is thrown.

4213.2.2 Explicit enumeration conversions

43The explicit enumeration conversions are:

135

C# LANGUAGE SPECIFICATION

1From sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, or decimal to

2any enum-type.

3From any enum-type to sbyte, byte, short, ushort, int, uint, long, ulong, char, float,

4double, or decimal.

5From any enum-type to any other enum-type.

6An explicit enumeration conversion between two types is processed by treating any participating enum-type

7as the underlying type of that enum-type, and then performing an implicit or explicit numeric conversion

8between the resulting types. [Example: Given an enum-type E with and underlying type of int, a conversion

9from E to byte is processed as an explicit numeric conversion (§13.2.1) from int to byte, and a

10conversion from byte to E is processed as an implicit numeric conversion (§13.1.2) from byte to int. end

11example]

1213.2.3 Explicit reference conversions

13The explicit reference conversions are:

14From object to any reference-type.

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

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

17implement T.

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

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

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

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

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

27interfaces, provided there is an explicit reference conversion from S to T.

28From System.Collections.Generic.IList<T> and its base interfaces to a one-dimensional

29array-type S[], provided there is an implicit or explicit reference conversion from S[] to

30System.Collections.Generic.IList<T>. This is precisely when either S and T are the same type

31or there is an implicit or explicit reference conversion from S to T.

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

33conversions exist:

34From the effective base class C of T to T and from any base class of C to T.

35From any interface-type to T.

36From T to any interface-type I provided there isn’t already an implicit reference conversion from T to I.

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

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

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

40The explicit reference conversions are those conversions between reference-types that require run-time

41checks to ensure they are correct.

136

Chapter 13 Conversions

1For an explicit reference conversion to succeed at run-time, the value of the source operand shall be null,

2or the actual type of the object referenced by the source operand shall be a type that can be converted to the

3destination type by an implicit reference conversion (§13.1.4). If an explicit reference conversion fails, a

4System.InvalidCastException is thrown.

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

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

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

813.2.4 Unboxing conversions

9An unboxing conversion permits an explicit conversion from type object or System.ValueType to any

10value-type, or from any interface-type to any value-type that implements the interface-type, and from the

11type System.Enum to any enumeration type. An unboxing operation consists of first checking that the

12object instance is a boxed value of the given value-type or enumeration type, and then copying the value out

13of the instance. An enum can be unboxed from the type System.Enum, since that is the direct base class for

14all enum types (§21.4). A struct or enum can be unboxed from the type System.ValueType, since that is

15the direct base class for all structs (§18.3.2) and a base class for all enums.

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

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

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

19executed as an explicit reference conversion or identity conversion.

20From the effective base class C of T to T and from any base class of C to T. [Note: C will be one of the

21types System.Object, System.ValueType, or System.Enum (otherwise T would be known to be a

22reference type and §13.2.3 would apply instead of this clause). end note]

23From any interface-type to T.

2413.2.5 Unboxing conversions are described further in §11.3.2.Explicit type parameter

25conversions

26This clause details explicit conversions involving type parameters that are not classified as explicit reference

27conversions or explicit unboxing conversions.

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

29exist:

30From T to any interface-type I provided there is not already an implicit conversion from T to I. This

31conversion consists of an implicit boxing conversion (§13.1.5) from T to object followed by an

32explicit reference conversion from object to I. At run-time, if T is a value type, the conversion is

33executed as a boxing conversion followed by an explicit reference conversion. At run-time, if T is a

34reference type, the conversion is executed as an explicit reference conversion.

35From a type parameter U to T provided that T depends on U (§26.7). At run-time, if T is a value type and

36U is a reference type, the conversion is executed as an unboxing conversion. At run-time, if both T and U

37are value types, then T and U are necessarily the same type and no conversion is performed. At run-time,

38if T is a reference type, then U is necessarily also a reference type and the conversion is executed as an

39explicit reference conversion or identity conversion.

4013.2.6 User-defined explicit conversions

41A user-defined explicit conversion consists of an optional standard explicit conversion, followed by

42execution of a user-defined implicit or explicit conversion operator, followed by another optional standard

43explicit conversion. The exact rules for evaluating user-defined conversions are described in §13.4.4.

137

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