Friday, April 15, 2011

Hidden features of Visual Studio, enter watch expressions with format specifiers

Every now and then I find myself getting annoyed at these little refresh squiggles that the Visual Studio watch expression get if they potentially have side-effects. I reckon that's a good thing, however, sometimes I need to express a quick way to drill down some data structure and get at that specific value.

Format Specifiers in C# (see MSDN)

These format specifiers are for C# but some of these are language agnostic and some variations of these work with other languages.

There's more information on MSDN. But these are my favourite ones, the ones I try to remember. Because when I need them, I have know that I can use them.

Specifier Format Value Displays
ac Force evaluation of an expression. This can be useful when implicit evaluation of properties and implicit function calls is turned off. *
d Decimal integer 0x0065 101
h Hexadecimal integer 61541 0x0000F065
nq String with No Quotes "My String" My String

* See Side Effects and Expressions.

And this is how you use them...

Name Value
Text,nq string without quotes
source.Skip(2).First(),ac The 3rd element of an enumerable source
0xa,d 10
10,h 0x0000000a

The d and h format specifiers are especially nice when you find yourself toggling Hexadecimal Display on and off. And as a bonus, If you'ven figured this out yourself, these expressions also work in the DebuggerDisplayAttribute string. Very handy.

No comments: