How is null + true a string?

前端 未结 7 677
后悔当初
后悔当初 2021-01-30 05:49

Since true is not a string type, how is null + true a string ?

string s = true;  //Cannot implicitly convert type \'bool\' to \'string\         


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 06:49

    The compiler goes out hunting for an operator+() that can take a null argument first. None of the standard value types qualify, null is not a valid value for them. The one and only match is System.String.operator+(), there's no ambiguity.

    The 2nd argument of that operator is also a string. That goes kapooey, cannot implicitly convert bool to string.

提交回复
热议问题