Search code examples
c#string.formatformatexception

System.FormatException of a string


I'm getting System.FormatException: Input string was not in a correct format when running this method.

The values of the fields:

arrayName = "requester";
fieldList = "\"name\" : \"shimshon\""; // "name" : "shimshon"


public override string ToString()
{
   var val = string.Format("\"{0}\" : { {1} }", arrayName, fieldList);

   return val;
}

the expect result of the method is

"requester" : { "name" : "shimshon" }

What is wrong with this format?


Solution

  • I think you want:

    var val = string.Format("\"{0}\" : {{ {1} }}", arrayName, fieldList);
    

    Note the doubled {{ and }} which is the escape sequence necessary to get braces literally into the output.