DDushaj DDushaj - 3 months ago 28
C# Question

String.Format crashes when passing obj[] from generic[] parameter

Ran into an interesting problem today, I changed some of my error generating methods to accept generically typed arrays as parameters instead of explicit string[]. However, when I tried to pass the generic parameter arrays to a String.Format call later in the method, it crashes it, and trying to access the array from within String.Format only returns 1 thing in the collection: the collection type.

Example:

// Just a small snippet of affected code
// Passing in 1 and a new string[] { "FIRST", "SECOND" }
public void SetErrorProperties(int errorCode, string[] paramArgs)
{
Dictionary<int, string> ErrorMessages = new Dictionary<int, string>(){ {1, "TEST MESSAGE: {0} {1}"} };
err_msg += String.Format(ErrorMessages.Index[errorCode], paramArgs);
//works just fine, returns "TESTING MESSAGE: FIRST SECOND"
}

public void SetErrorProperties<T>(int errorCode, T[] paramArgs)
{
Dictionary<int, string> ErrorMessages = new Dictionary<int, string>(){ {1, "TEST MESSAGE: {0} {1}"} };
err_msg += String.Format(ErrorMessages.Index[errorCode], paramArgs);
// now this returns an error of Format Exception: Index (zero based) must be greater than or equal to zero and less than the size of the argument list

// accessing array directly paramArgs[0] returns "FIRST" and paramArgs[1] returns "SECOND"
// However, String.Format("{0}", paramArgs) returns "System.String[]" and String.Format ("{0} {1}", paramArgs) return the index exception
}


Does anyone know why this happens when String.Format accepts an object[] as the second parameter? Thanks!

Answer

why this happens when String.Format accepts an object[] as the second parameter?

This is exactly why it is happening - T[] is not necessarily an object[], so unless T happens to be object in a particular call, the cast fails.

You can fix this by converting to object[] inside the call, like this:

err_msg += String.Format(ErrorMessages.Index[errorCode], paramArgs.Cast<object>().ToArray());