Alex Rivera | Logout

string.Format() parameters

Asked 2009-02-18T13:19:53.207
16

How many parameters can you pass to a string.Format() method?

There must be some sort of theoretical or enforced limit on it. Is it based on the limits of the params[] type or the memory usage of the app that is using it or something else entirely?

Edit
Report

1 Answer

3

Considering that both the limit of the Array class and the String class are the upper limit of Int32 (documented at 2,147,483,647 here: Int32 Structure), it is reasonable to believe that this value is the limit of the number string of format parameters.

Update Upon checking reflector, John is right. String.Format, using the Red Gate Reflector, shows the ff:

public static string Format(IFormatProvider provider, string format, params object[] args)
{
    if ((format == null) || (args == null))
    {
        throw new ArgumentNullException((format == null) ? "format" : "args");
    }
    StringBuilder builder = new StringBuilder(format.Length + (args.Length * 8));
    builder.AppendFormat(provider, format, args);
    return builder.ToString();
}

The format.Length + (args.Length * 8) part of the code is enough to kill most of that number. Ergo, '2,147,483,647 = x + 8x' leaves us with x = 238,609,294 (theoretical).

It's far less than that of course; as the guys in the comments mentioned the string hitting the string length limit earlier is quite likely.

Maybe someone should just code this into a machine problem! :P

answered 2009-02-18T13:34:06.633

Your Answer