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!