Question regarding implicit conversions in the C# language specification

前端 未结 4 855
渐次进展
渐次进展 2021-02-08 08:15

Section 6.1 Implicit conversions defines an identity conversion thusly:

An identity conversion converts from any type to the sam

4条回答
  •  我在风中等你
    2021-02-08 08:46

    May it is such that the code guarantees pass-through when called like Convert.ChangeType(client, typeof(Client)) regardless if IConvertible is implemented.

    Look into the source of ChangeType from mscorlib with Reflector and notice the conditions at which value is returned as-is.

    Remember a = operator is not a conversion, just a reference set. So code like Client client_2 = client_1 does not perform any implicit conversions. If an identity implicit conversion is declared then error CS0555 is issued.

    I guess the spec says let the C# compiler handle such cases, and thus dot not manually try to define identity conversions.

提交回复
热议问题