这个问题已经有了答案:
Why does Boolean.ToString output “True” and not “true”
8个答案
我是从Joseph Albahari和Ben Albahari简而言之从C 5中学习拳击和解题的。版权所有2012 Joseph Albahari和Ben Albahari,978-1-449-32010-2,但我需要扩展知识的深度,我找到了MSDN文章:Boxing and Unboxing (C# Programming Guide),在上面我找到了这个示例代码(显然与主要主题没有内在联系):

Console.WriteLine (String.Concat("Answer", 42, true));

执行后返回:
Answer42True

为什么这是发生在字面上的'true'(同样发生在'false')?
Execution test
提前谢谢。

最佳答案

出于示例原因,如果您尝试在mscorlib.dll中反编译String.Concat()方法
你会得到这样的东西

      for (int index = 0; index < args.Length; ++index)
      {
        object obj = args[index];
        values[index] = obj == null ? string.Empty : obj.ToString(); //which  will call the `ToString()` of `boolean struct`

      }

ToString()方法,默认情况下由string.Concat方法调用,如下所示
 public override string ToString()
    {
      return !this ? "False" : "True";
    }

09-25 22:21
查看更多