string.Format fails at runtime with array of integers

前端 未结 7 576
囚心锁ツ
囚心锁ツ 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:58

    Your string.Format is expecting 2 arguments ({0} and {1}). You are only supplying 1 argument (the int[]). You need something more like this:

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

    The compiler does not notice the problem because the format string is evaluated at runtime. IE The compiler doesn't know that {0} and {1} mean there should be 2 arguments.

提交回复
热议问题