Refactor ValueTuple ToString to use string interpolation#124380
Refactor ValueTuple ToString to use string interpolation#124380sonnemaf wants to merge 1 commit intodotnet:mainfrom
Conversation
Updated ToString and ToStringEnd methods for ValueTuple types (1-8 elements) to use C# string interpolation instead of string concatenation. This improves code readability and conciseness by directly embedding tuple items in the output string.
There was a problem hiding this comment.
Pull request overview
This PR refactors the ToString and ToStringEnd methods across all ValueTuple types (1-8 elements) to use C# string interpolation instead of string concatenation. The refactoring maintains behavioral equivalence while improving code readability and potentially reducing heap allocations through the use of modern interpolated string handlers.
Changes:
- Replaced string concatenation with string interpolation in
ToString()methods for ValueTuple through ValueTuple - Replaced string concatenation with string interpolation in
ToStringEnd()methods for all ValueTuple variants - Maintained exact same null handling behavior (null values are represented as empty strings)
| public override string ToString() | ||
| { | ||
| return "(" + Item1?.ToString() + ", " + Item2?.ToString() + ")"; | ||
| return $"({Item1}, {Item2})"; |
There was a problem hiding this comment.
The code size of ValueTuple instantiations is a problem for AOT. This change will make the ValueTuple instantiations even more expensive in terms of code size than what they are today.
There was a problem hiding this comment.
Yeah, it would be a size regression: MichalStrehovsky/rt-sz#204 (comment)
So the question is whether this is worth the compromise in terms of code size. I would lean towards no. The ToString on ValueTuple is more of a convenience method for debugging. I guess there are some uses where the default stringification (braces, comma, unescaped ToString of the inner item) is useful for perf-critical business logic, but I would expect most people don't have a use for this.
Updated
ToStringandToStringEndmethods forValueTupletypes (1-8 elements) to use C# string interpolation instead of string concatenation. This improves code readability and conciseness by directly embedding tuple items in the output string. It can also improve the performance due to less heap allocations.Not sure if these types should also implement ISpanFormattable. That could improve the performance even more.