13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13 Conversions13.4.2 Evaluation of user-defined conversionsA user-defined conversion converts a value from its type, called the source type, to another type, called thetarget type. Evaluation of a user-defined conversion centers on finding the most specific user-definedconversion operator for the particular source and target types. This determination is broken into severalsteps:• Finding the set of classes and structs from which user-defined conversion operators will be considered.This set consists of the source type and its base classes and the target type and its base classes (with theimplicit assumptions that only classes and structs can declare user-defined operators, and that non-classtypes have no base classes).• From that set of types, determining which user-defined conversion operators are applicable. For aconversion operator to be applicable, it must be possible to perform a standard conversion (§13.3) fromthe source type to the operand type of the operator, and it must be possible to perform a standardconversion from the result type of the operator to the target type.• From the set of applicable user-defined operators, determining which operator is unambiguously themost specific. In general terms, the most specific operator is the operator whose operand type is“closest” to the source type and whose result type is “closest” to the target type. The exact rules forestablishing the most specific user-defined conversion operator are defined in the following sections.Once a most specific user-defined conversion operator has been identified, the actual execution of the userdefinedconversion involves up to three steps:• First, if required, performing a standard conversion from the source type to the operand type of the userdefinedconversion operator.• Next, invoking the user-defined conversion operator to perform the conversion.• Finally, if required, performing a standard conversion from the result type of the user-definedconversion operator to the target type.Evaluation of a user-defined conversion never involves more than one user-defined conversion operator. Inother words, a conversion from type S to type T will never first execute a user-defined conversion from S toX and then execute a user-defined conversion from X to T.Exact definitions of evaluation of user-defined implicit or explicit conversions are given in the followingsections. The definitions make use of the following terms:• If a standard implicit conversion (§13.3.1) exists from a type A to a type B, and if neither A nor B areinterface-types, then A is said to be encompassed by B, and B is said to encompass A.• The most encompassing type in a set of types is the one type that encompasses all other types in the set.If no single type encompasses all other types, then the set has no most encompassing type. In moreintuitive terms, the most encompassing type is the “largest” type in the set—the one type to which eachof the other types can be implicitly converted.• The most encompassed type in a set of types is the one type that is encompassed by all other types in theset. If no single type is encompassed by all other types, then the set has no most encompassed type. Inmore intuitive terms, the most encompassed type is the “smallest” type in the set—the one type that canbe implicitly converted to each of the other types.13.4.3 User-defined implicit conversionsA user-defined implicit conversion from type S to type T is processed as follows:• Find the set of types, D, from which user-defined conversion operators will be considered. This setconsists of S (if S is a class or struct), the base classes of S (if S is a class), T (if T is a class or struct),and the base classes of T (if T is a class).• Find the set of applicable user-defined conversion operators, U. This set consists of the user-definedimplicit conversion operators declared by the classes or structs in D that convert from a type119

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!