Tuesday, November 27, 2012

.Net under the covers

An interesting little oddity I stumbled across the other day, involving StringBuilder.AppendFormat, and a string which it turned out had had a GUID already expanded into it, and an unexpected exception result

open System
open System.Text
let sb = new StringBuilder()
// This is the case that surprised me
sb.AppendFormat("{4f2616db-6e2a-44ca-b2d8-f71ddf94d9d6}");;
System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument
list.
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at .$FSI_0009.main@()
Stopped due to error
// This is the result that I had expected
sb.AppendFormat("{4f2616db-6e2a-44ca-b2d8-f71ddf94d9d6}", 0, 1, 2, 3, 4);;
System.FormatException: Input string was not in a correct format.
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at .$FSI_0012.main@()
Stopped due to error
view raw gistfile1.fs hosted with ❤ by GitHub

so it looks like the parse for a number is run before checking that the format is even sane; not what one would have expected.

No comments :