-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add side-effect free debugger display to ValueStringBuilder.
#122265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Calling `ToString()` clears the builder, which can cause surprises when debugging.
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a side-effect free debugger display to both versions of ValueStringBuilder to address the problematic behavior where calling ToString() clears the builder, which causes unexpected issues during debugging.
Key changes:
- Added
[DebuggerDisplay]attribute to bothValueStringBuilderandValueStringBuilder<TChar>structs - Created side-effect free
DebuggerDisplayproperties that display the builder's content without clearing it - Refactored
ValueStringBuilder<TChar>.ToString()to extract string generation logic into a separateGetString()method
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/Common/src/System/Text/ValueStringBuilder.cs | Added [DebuggerDisplay] attribute and DebuggerDisplay property that uses AsSpan().ToString() for side-effect free debugging |
| src/libraries/Common/src/System/Text/ValueStringBuilder_1.cs | Added [DebuggerDisplay] attribute, extracted GetString() method for reuse, and added DebuggerDisplay property that calls GetString() for side-effect free debugging |
| } | ||
|
|
||
| // ToString() clears the builder, so we need a side-effect free debugger display. | ||
| private string DebuggerDisplay => AsSpan().ToString(); |
Copilot
AI
Dec 6, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DebuggerDisplay property should be marked as readonly for consistency with the generic version in ValueStringBuilder_1.cs and to clearly indicate it doesn't mutate state. The property only reads from the builder without modifying it.
private readonly string DebuggerDisplay => AsSpan().ToString();| private string DebuggerDisplay => AsSpan().ToString(); | |
| private readonly string DebuggerDisplay => AsSpan().ToString(); |
Calling
ToString()clears the builder, which can cause surprises when debugging.