string.Format fails at runtime with array of integers

前端 未结 7 564
囚心锁ツ
囚心锁ツ 2020-12-31 00:03

Consider string.Format() whose parameters are a string and, among others in the overload list, an object[] or many objects.

This statement

7条回答
  •  無奈伤痛
    2020-12-31 00:57

    This works:

    string bar = string.Format("{0} and {1}", myInts[0], myInts[1]);
    

    The compiler doesn't catch it because it doesn't evaluate your format string.

    The example you gave up top doesn't match what you're trying to do down below... you provided two {} and two arguments, but in the bottom one you only provided one argument.

提交回复
热议问题