Skip to content

Refactor ValueTuple ToString to use string interpolation#124380

Open
sonnemaf wants to merge 1 commit intodotnet:mainfrom
sonnemaf:UseStringInterpolation
Open

Refactor ValueTuple ToString to use string interpolation#124380
sonnemaf wants to merge 1 commit intodotnet:mainfrom
sonnemaf:UseStringInterpolation

Conversation

@sonnemaf
Copy link
Contributor

@sonnemaf sonnemaf commented Feb 13, 2026

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. 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.

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.
Copilot AI review requested due to automatic review settings February 13, 2026 12:54
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Feb 13, 2026
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Feb 13, 2026
Copy link
Contributor

Copilot AI left a 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 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})";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates that the PR has been added by a community member needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants