martin36 martin36 - 9 days ago 7
C# Question

Using an array as argument for string.Format()

When trying to use an array as an argument for the

string.Format()
method, I get the following error:


FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.


The code is as follows:

place = new int[] { 1, 2, 3, 4};
infoText.text = string.Format("Player1: {0} \n Player2: {1} \n Player3: {2} \n Player4: {3}", place);


The Array contains four values and the arguments in the
String.Format()
are also the same.

What causes this error?

(The
infoText.text
is just a regular String object)

Answer

You can convert int array to string array as pass it.

infoText.text = string.Format("Player1: {0} \n Player2: {1} \n Player3: {2} \n Player4: {3}", 
                              place.Select(x=>x.ToString()).ToArray());