14.4.2.3 Better conversion

本文详细介绍了C#中两种从类型S到类型T1和T2的隐式转换C1和C2之间的比较规则。通过这些规则,可以确定哪种转换更优,并解释了在不同情况下如何判断一种转换优于另一种。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given an implicit conversion C1 that converts from a type S to a type T1,
and an implicit conversion C2 that
converts from a type S to a type T2, the better conversion of the two
conversions is determined as follows:
?If T1 and T2 are the same type, neither conversion is better.
?If S is T1, C1 is the better conversion.
?If S is T2, C2 is the better conversion.
?If an implicit conversion from T1 to T2 exists, and no implicit
conversion from T2 to T1 exists, C1 is the
better conversion.
?If an implicit conversion from T2 to T1 exists, and no implicit
conversion from T1 to T2 exists, C2 is the
better conversion.
?If T1 is sbyte and T2 is byte, ushort, uint, or ulong, C1 is the better
conversion.
?If T2 is sbyte and T1 is byte, ushort, uint, or ulong, C2 is the better
conversion.
?If T1 is short and T2 is ushort, uint, or ulong, C1 is the better
conversion.
C# LANGUAGE SPECIFICATION
136
?If T2 is short and T1 is ushort, uint, or ulong, C2 is the better
conversion.
?If T1 is int and T2 is uint, or ulong, C1 is the better conversion.
?If T2 is int and T1 is uint, or ulong, C2 is the better conversion.
?If T1 is long and T2 is ulong, C1 is the better conversion.
?If T2 is long and T1 is ulong, C2 is the better conversion.
?Otherwise, neither conversion is better.
If an implicit conversion C1 is defined by these rules to be a better
conversion than an implicit conversion C2,
then it is also the case that C2 is a worse conversion than C1.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值