Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index (zero based) error when using verbatim string literal and appendformat

I'm trying to use string builder and append format to build some html. How can I use the verbatim string literal and append format without getting the following error: "Index (zero based) must be greater than or equal to zero and less than the size of the argument list."

See this example:

StringBuilder sb = new StringBuilder(5469);<br />
sb.AppendFormat(@"<td width=""155"">{0}</td>", localVariable); // zero index error here <br />
return sb;
like image 937
Victor Avatar asked Jun 09 '26 10:06

Victor


1 Answers

Is the type of localVariable an array? If so the array is being interpreted as a params array instead of the first argument to a params array. To fix this you need to explicitly cast it to object when calling AppendFormat

sb.AppendFormat(@"{0}", (object)localVariable);
like image 186
JaredPar Avatar answered Jun 12 '26 01:06

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!