31

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)

4
  • 3
    You're passing an int[] as opposed to a string[] Commented Nov 30, 2016 at 9:58
  • Perhaps something like string.Format("Player1: {0} \n Player2: {1} \n Player3: {2} \n Player4: {3}", place[0], place[1], place[2], place[3]); It works as you pass an object relating every {} in string Commented Nov 30, 2016 at 10:01
  • 3
    Just for the record, instead of using string.format, you could create the string more dynamically without hardcoding the number of players, e.g.: infoText.text = string.Join("\n", place.Select((p,i) => $"Player{i+1}: {p}")); Commented Nov 30, 2016 at 10:07
  • @Me.Name That looks really good. I will try that. Thanks! Commented Nov 30, 2016 at 10:18

4 Answers 4

28

You can convert int array to string array as pass it using System.Linq Select() extension method.

infoText.text = string.Format("Player1: {0} \nPlayer2: {1} \nPlayer3: {2} \nPlayer4: {3}", 
                              place.Select(x => x.ToString()).ToArray());

Edit:

In C# 6 and above, you can also able to use String Interpolation instead of using string.Format()

infoText.text = $"Player1: {place[0]}\nPlayer2: {place[1]} \nPlayer3: {place[2]} \nPlayer4: {place[3]}";

Check this fiddle for your reference.

Sign up to request clarification or add additional context in comments.

2 Comments

So the argument can be a string[] array but not an int[] array?
@martin36 not only string[] - your array should be an object or string type.
11

Quick fix.

var place = new object[] { 1, 2, 3, 4 };

C# does not support co-variant array conversion from int[] to object[] therefor whole array is considered as object, hence this overload with a single parameter is called.

2 Comments

C# does support co-variant array conversion (e.g. from string[] to object[]), but in this case the conversion would also involve boxing of value types which is why it won't work.
@Joey: he wrote "does not support co-variant array conversion from int[] to object[]" ;-)
6

It is possible to pass an explicit array for a params argument, but it has to have the matching type. string.Format has a few overloads, of which the following two are interesting to us:

string.Format(string, params object[])
string.Format(string, object)

In your case treating the int[] as object is the only conversion that works, since an int[] cannot be implicitly (or explicitly) converted to object[], so string.Format sees four placeholders, but only a single argument. You'd have to declare your array of the correct type

var place = new object[] {1,2,3,4};

2 Comments

What is the difference between params object[] and just a regular object[] array?
@martin36: You can treat the params parameter as any number of arguments. The compiler will automatically wrap them in an array to call the method.
3

As others have already said, you can't convert int[] to object[]. But you can fix this issue using Enumerable.Cast<T>():

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

BTW, if you're on C# 6 or above, you might consider using interpolated strings instead of string.Format:

infoText.text = $"Player1: {place[0]}\n Player 2: {place[1]} \n Player 3: {place[2]} \n Player 4: {place[3]}";

3 Comments

I am using Unity and it has only the version C# 2.
Oh sorry, but thanks for the answer anyways. I will remember it for later use.
@martin36 No problem, it might be an useful suggestion for other readers too :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.