第 6.1 节隐式转换 如此定义身份转换:
An identity conversion converts from any type to the same type. This conversion exists such that an entity that already has a required type can be said to be convertible to that type.
现在,这些句子的目的是什么?
(In §6.1.6 Implicit reference conversions)
The implicit reference conversions are:
- [...]
- From any reference-type to a reference-type
T
if it has an implicit identity or reference conversion to a reference-typeT
0 andT
0 has an identity conversion toT
.
和:
(In §6.1.7 Boxing conversions)
- A value type has a boxing conversion to an interface type
I
if it has a boxing conversion to an interface typeI
0 andI
0 has an identity conversion toI
.
最初它们似乎是多余的(同义反复)。但它们必须是有目的的,那么它们为什么会在那里呢?
你能举出两种类型的例子
T
1、T
2 使得 T
1 不会隐式转换为 T
2 如果不是上面引用的段落?
最佳答案
规范的第 4.7 节指出,存在来自 Foo<dynamic>
的身份转换。至 Foo<object>
反之亦然。您引用的规范部分是为了确保处理这种情况而编写的。也就是说,如果存在从 T 到 C<object, object>
的隐式引用转换然后还有对 C<object, dynamic>
的隐式引用转换, C<dynamic, object>
和 C<dynamic, dynamic>
.
人们可能会合理地指出 (1) 这些短语的意图是不明显的 - 因此你的问题 - 并且令人困惑,以及 (2) 关于身份转换的部分应该交叉引用关于动态转换的部分,以及 (3) 短语像这样的规范使得规范的实现者很难将规范语言清楚地转换为实现。如何知道是否存在任何此类类型?规范不需要指定确切的算法,但如果提供更多指导就更好了。
遗憾的是,该规范并不是一份完美的文档。
关于c# - 关于 C# 语言规范中隐式转换的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3736789/