How to Resolve Error "CS0726: 'format specifier' is not a valid format specifier" in C#
The Compiler Error CS0726 is a syntax error related specifically to Debugging Attributes, such as [DebuggerDisplay]. The message reads: "'specifier' is not a valid format specifier".
In C#, the DebuggerDisplay attribute allows you to customize how an object appears in the Visual Studio debugger windows (Watch, Autos, Locals). Inside the interpolation braces {variable, specifier}, you can use special codes (like nq for "No Quotes") to control the output format. This error occurs when you use a character after the comma that the debugger does not recognize.
This guide explains the valid debugger specifiers and how to distinguish them from standard string formatting.
Understanding Debugger Specifiers
When defining a display string for the debugger:
- Expression:
{MyProperty} - Expression with Debugger Specifier:
{MyProperty, nq}
The comma , signals to the compiler that what follows is a debugger instruction, not a standard string format. The list of allowed instructions is very short (e.g., nq, h, raw). If you type {MyProperty, C} expecting currency formatting, the compiler sees C, realizes it is not a known debugger instruction, and throws CS0726.
Scenario 1: Using Invalid Codes in DebuggerDisplay
This error most commonly happens when a developer types a random character or a typo after the comma inside the attribute string.
Example of error
using System.Diagnostics;
// ⛔️ Error CS0726: 'txt' is not a valid format specifier.
// The compiler does not know what 'txt' means in this context.
[DebuggerDisplay("Name = {Name, txt}")]
public class User
{
public string Name { get; set; }
}
Solution: Use Valid Codes (or Remove It)
If you just want to display the property, remove the comma. If you wanted to remove the quotes around the string, use nq.
using System.Diagnostics;
// ✅ Correct: 'nq' (No Quotes) is a valid specifier.
[DebuggerDisplay("Name = {Name, nq}")]
public class User
{
public string Name { get; set; }
}
Scenario 2: Confusing String Format (:) with Debugger Format (,)
In standard string.Format or Console interpolation, we use the colon : to specify formatting (e.g., {Price:C} for Currency, {Date:d} for Short Date).
In DebuggerDisplay, the comma , is reserved for Debugger Instructions. If you try to use standard formatting codes with a comma, it fails.
Example of error
Trying to format a number as Hexadecimal using ,x (which looks like C++ syntax) or Currency using ,C.
public class Product
{
public decimal Price { get; set; }
// ⛔️ Error CS0726: 'C' is not a valid format specifier.
// The comma tells the debugger "Here is an instruction", but 'C' is not an instruction.
[DebuggerDisplay("Cost: {Price, C}")]
public void Show() { }
}
Solution: Use Expressions for Formatting
If you want to format the value (e.g., as Currency), do it inside the expression part using .ToString(), or check if a valid debugger specifier exists (like h for simple hex).
// ✅ Correct: We perform the formatting logic inside the expression braces.
[DebuggerDisplay("Cost: {Price.ToString(\"C\")}")]
public class Product
{
public decimal Price { get; set; }
}
List of Valid Specifiers
When using the comma syntax {Value, code}, only the following codes are valid:
| Specifier | Description | Example |
|---|---|---|
| nq | No Quotes. Use for strings to remove the surrounding "". | {Name, nq} |
| h | Hexadecimal. Displays integers in hex format. | {ID, h} |
| ac | Ancestor Chain. Shows the inheritance chain. | {this, ac} |
| raw | Raw View. Displays the raw item ignoring proxy attributes. | {this, raw} |
| d | Decimal. Forces integer display as decimal (if hex is default). | {ID, d} |
Conclusion
CS0726 is the compiler enforcing the limited vocabulary of the debugger attributes.
- Check the Separator: Are you using a comma
,? If so, you must use one of the specific debugger codes (nq,h, etc.). - Check Intent:
- If you want Formatting (Currency, Date, padding): Use the property's
.ToString()method inside the braces. - If you want Display Options (Hex, No Quotes): Use the comma syntax.
- If you want Formatting (Currency, Date, padding): Use the property's