string.Format fails at runtime with array of integers

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

    I think the concept you are having an issue with is why int[] isn't cast to object[]. Here's an example that shows why that would be bad

    int[] myInts = new int[]{8,9};
    object[] myObjs = (object[])myInts;
    myObjs[0] = new object();
    

    The problem is that we just added an object into a int array.

    So what happens in your code is that myInts is cast to object and you don't have a second argument to fill in the {1}

提交回复
热议问题