分享
 
 
 

13.4 User-defined conversions

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

C# allows the pre-defined implicit and explicit conversions to be augmented

by user-defined conversions.

User-defined conversions are introduced by declaring conversion operators (?

7.9.3) in class and struct

types.

13.4.1 Permitted user-defined conversions

C# permits only certain user-defined conversions to be declared. In

particular, it is not possible to redefine

an already existing implicit or explicit conversion. A class or struct is

permitted to declare a conversion from

a source type S to a target type T only if all of the following are true:

?S and T are different types.

?Either S or T is the class or struct type in which the operator

declaration takes place.

?Neither S nor T is object or an interface-type.

?T is not a base class of S, and S is not a base class of T.

The restrictions that apply to user-defined conversions are discussed

further in ?7.9.3.

13.4.2 Evaluation of user-defined conversions

A user-defined conversion converts a value from its type, called the source

type, to another type, called the

target type. Evaluation of a user-defined conversion centers on finding the

most specific user-defined

conversion operator for the particular source and target types. This

determination is broken into several

steps:

?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 the

implicit assumptions that only classes and structs can declare user-defined

operators, and that non-class

types have no base classes).

?From that set of types, determining which user-defined conversion

operators are applicable. For a

conversion operator to be applicable, it must be possible to perform a

standard conversion (?3.3) from

the source type to the operand type of the operator, and it must be

possible to perform a standard

conversion from the result type of the operator to the target type.

?From the set of applicable user-defined operators, determining which

operator is unambiguously the

most 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 for

establishing 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 userdefined

conversion involves up to three steps:

?First, if required, performing a standard conversion from the source type

to the operand type of the userdefined

conversion 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-defined

conversion operator to the target type.

Evaluation of a user-defined conversion never involves more than one

user-defined conversion operator. In

other words, a conversion from type S to type T will never first execute a

user-defined conversion from S to

X 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 following

sections. The definitions make use of the following terms:

?If a standard implicit conversion (?3.3.1) exists from a type A to a

type B, and if neither A nor B are

interface-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 more

intuitive terms, the most encompassing type is the ?largest? type in the

set?the one type to which each

of 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 the

set. If no single type is encompassed by all other types, then the set has

no most encompassed type. In

more intuitive terms, the most encompassed type is the ?smallest? type in

the set?the one type that can

be implicitly converted to each of the other types.

13.4.3 User-defined implicit conversions

A 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 set

consists 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-defined

implicit conversion operators declared by the classes or structs in D that

convert from a type

encompassing S to a type encompassed by T. If U is empty, the conversion is

undefined and a compiletime

error occurs.

?Find the most specific source type, SX, of the operators in U:

If any of the operators in U convert from S, then SX is S.

Otherwise, SX is the most encompassed type in the combined set of source

types of the operators in U. If no

most encompassed type can be found, then the conversion is ambiguous and a

compile-time error occurs.

?Find the most specific target type, TX, of the operators in U:

If any of the operators in U convert to T, then TX is T.

Otherwise, TX is the most encompassing type in the combined set of target

types of the operators in U. If no

most encompassing type can be found, then the conversion is ambiguous and a

compile-time error occurs.

?If U contains exactly one user-defined conversion operator that converts

from SX to TX, then this is the

most specific conversion operator. If no such operator exists, or if more

than one such operator exists,

then the conversion is ambiguous and a compile-time error occurs.

Otherwise, the user-defined

conversion is applied:

If S is not SX, then a standard implicit conversion from S to SX is

performed.

The most specific user-defined conversion operator is invoked to convert

from SX to TX.

If TX is not T, then a standard implicit conversion from TX to T is

performed.

13.4.4 User-defined explicit conversions

A user-defined explicit 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 set

consists 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-defined

implicit or explicit conversion operators declared by the classes or

structs in D that convert from a type

encompassing or encompassed by S to a type encompassing or encompassed by

T. If U is empty, the

conversion is undefined and a compile-time error occurs.

?Find the most specific source type, SX, of the operators in U:

If any of the operators in U convert from S, then SX is S.

Otherwise, if any of the operators in U convert from types that encompass

S, then SX is the most

encompassed type in the combined set of source types of those operators. If

no most encompassed type can

be found, then the conversion is ambiguous and a compile-time error occurs.

Otherwise, SX is the most encompassing type in the combined set of source

types of the operators in U. If no

most encompassing type can be found, then the conversion is ambiguous and a

compile-time error occurs.

?Find the most specific target type, TX, of the operators in U:

If any of the operators in U convert to T, then TX is T.

Otherwise, if any of the operators in U convert to types that are

encompassed by T, then TX is the most

encompassing type in the combined set of target types of those operators.

If no most encompassing type can

be found, then the conversion is ambiguous and a compile-time error occurs.

Otherwise, TX is the most encompassed type in the combined set of target

types of the operators in U. If no

most encompassed type can be found, then the conversion is ambiguous and a

compile-time error occurs.

?If U contains exactly one user-defined conversion operator that converts

from SX to TX, then this is the

most specific conversion operator. If no such operator exists, or if more

than one such operator exists,

then the conversion is ambiguous and a compile-time error occurs.

Otherwise, the user-defined

conversion is applied:

If S is not SX, then a standard explicit conversion from S to SX is

performed.

The most specific user-defined conversion operator is invoked to convert

from SX to TX.

If TX is not T, then a standard explicit conversion from TX to T is

performed.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有