5

If I specify a (date) format on the [DebuggerDisplay], I see a error CS0726:

error CS0726: ':d' is not a valid format specifier

For example this code:

[DebuggerDisplay("{From:d} - {To:d}")
public class DateRange 
{
    public DateTime From { get; set; }
    public DateTime To { get; set; }
}

Shows when debugging in Visual Studio:

enter image description here

Julian
  • 33,915
  • 22
  • 119
  • 174

1 Answers1

3

For specifying the format on the [DebuggerDisplay] you need an expression, e.g. ToString("d") - and escape the quotes.

This works:

[DebuggerDisplay("{From.ToString(\"d\"),nq} - {To.ToString(\"d\"),nq}")
public class DateRange 
{
    public DateTime From { get; set; }
    public DateTime To { get; set; }
}

I also added a ,nq so we don't render extra quotes.

See Using Expressions in DebuggerDisplay

Result:

enter image description here

Note: ,d won't work for specifying the format - It won't give an error but I also won't change the format

Julian
  • 33,915
  • 22
  • 119
  • 174
  • I prefer to override the ToString(), which is possible in many languages. But, in case I need some different string for debugging purposes, then only I consider using DebuggerDisplay. Not sure if it is not recommended or a bad practice, but I hate to be forced to deal with different unreadable sintaxis, inside the same source code file. – zameb Mar 24 '23 at 23:28