You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
string.Create currently has two overloads when you have some knowledge about the length of the final string.
string Create<TState>(int length, TState state, SpanAction<char, TState> action) takes as its first parameter the length of the final string and via the action gives you access to fill in the string.
You as a developer has the responsibility to fill out the entire string - not more, not less.
string Create(IFormatProvider? provider, Span<char> initialBuffer, [InterpolatedStringHandlerArgument(nameof(provider), nameof(initialBuffer))] ref DefaultInterpolatedStringHandler handler) takes as the second parameter a buffer Span<char> which is filled by the handler and ultimately copied into the final string.
Only the written parts of the buffer is copied to the final string.
(And if you out-grow the buffer one is rented from ArrayPool<char>)
I was looking at #50635 and the recorded review, but couldn't find if there ever was a discussion about a variant that populates the final string directly via a string interpolation handler?
The overload I'm imagining would look something like
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
string.Create
currently has two overloads when you have some knowledge about the length of the final string.string Create<TState>(int length, TState state, SpanAction<char, TState> action)
takes as its first parameter the length of the final string and via theaction
gives you access to fill in the string.You as a developer has the responsibility to fill out the entire string - not more, not less.
string Create(IFormatProvider? provider, Span<char> initialBuffer, [InterpolatedStringHandlerArgument(nameof(provider), nameof(initialBuffer))] ref DefaultInterpolatedStringHandler handler)
takes as the second parameter a bufferSpan<char>
which is filled by thehandler
and ultimately copied into the final string.Only the written parts of the buffer is copied to the final string.
(And if you out-grow the buffer one is rented from
ArrayPool<char>
)I was looking at #50635 and the recorded review, but couldn't find if there ever was a discussion about a variant that populates the final string directly via a string interpolation handler?
The overload I'm imagining would look something like
Say you know that the final string will always be of length 24, this new overload will enable you to write
compared to the existing overloads that either write to an intermediate buffer or are quite verbose.
In https://www.reddit.com/r/dotnet/comments/15dr3gk/string_concatenation_benchmarks_in_net_8/juj40up/ I did some benchmarks, and it seemed the suggested overload could fill a gap between the existing overloads.
... or am I just missing an existing API that does exactly this?
Beta Was this translation helpful? Give feedback.
All reactions