If an identity conversion exists from S to T, must it be that S and T are same type?

前端 未结 1 1961
你的背包
你的背包 2021-02-19 22:43

In 6.1.6. of the C# language specification, there is:

The implicit reference conversions are:

(...)
From any reference-type to a referen

相关标签:
1条回答
  • 2021-02-19 22:55

    If an identity conversion exists from S to T, must it be that S and T are same type?

    The oddity you've discovered in the spec arose as a result of adding dynamic to the language in C# 4.0. At runtime there is no such thing as dynamic; rather, dynamic is just a type that means "I'm really object; please defer analysis of this portion of the program until runtime".

    Therefore there is an identity conversion between, say, List<object> and List<dynamic>. From the C# compiler's perspective they are different types because myList[0].Frob() would give an error for the former but not the latter. But from the runtime's perspective they are identical. Therefore the C# language classifies the conversion from one to the other as an identity conversion. At compile time the types can be different for the purposes of the C# language, but from the runtime's perspective they will be identical.

    0 讨论(0)
提交回复
热议问题