1

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!

DDushaj
  • 127
  • 8

1 Answers1

2

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());
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • ah ok thanks for clearing up the confusion. So is T in this context just treated as some generic...."thing" that isn't an object? – DDushaj Aug 09 '16 at 15:03
  • 1
    @DDushaj `T` may or may not correspond to `object`, depending on the way the method is called. The actual type is supplied at compile time: if you call `SetErrorProperties(1, 2, "hello", 45.67)` your call would succeed, because this makes `T` an `object`. – Sergey Kalinichenko Aug 09 '16 at 15:17