Originally posted by Nick Heppleston at: http://www.modhul.com/2009/03/19/interesting-quirk-when-formatting-guids-as-strings/
I noticed an interesting quirk last night while trying to format a Guid into a string representation – by default, Guid.ToString() does not return an accurate representation of a GUID with curly braces. Hmmmmmm…..
Consider the following simple test, here we are instantiating a new Guid and before outputting the value to the debugger, we capture its value – note the curly braces at the end of the Guid:
If we let the test run its course, we get the Guid formatted as a string (using the default formatter) written to the debugger, but there are no curly braces:
After much head scratching and Googling, I decided to look at the MSDN documentation for the Guid.ToString() method – low and behold there are several format specifiers that can be provided to the ToString() method which determine the formatting provided to the resultant string. Using the format specifiers:
we get the desired output:
If no format specifier is used (i.e. we just Guid.ToString()), the default format is ‘D’ – no braces.