How is null + true a string?

前端 未结 7 675
后悔当初
后悔当初 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:50

    null will be cast to null string, and there is implicit converter from bool to string so the true will be cast to string and then, + operator will be applied: it's like: string str = "" + true.ToString();

    if you check it with Ildasm:

    string str = null + true;

    it's as bellow:

    .locals init ([0] string str)
      IL_0000:  nop
      IL_0001:  ldc.i4.1
      IL_0002:  box        [mscorlib]System.Boolean
      IL_0007:  call       string [mscorlib]System.String::Concat(object)
      IL_000c:  stloc.0
    
    0 讨论(0)
提交回复
热议问题