Open
Description
Background and motivation
I'm wondering why there are no generic overloads for String.Manipulation
APIs like String.Concat or String.Join that accept ReadOnlySpan<T>
. Is this due to concerns about the complexity of overload resolution for the compiler?
Recently I've been working on params T[]
upgrading to params ReadOnlySpan<T>
. For exmple,
private static string CombineCacheKey(string prefix, string str) => $"{prefix}:{str}";
private static string CombineCacheKey(string prefix, params int[] ids) => CombineCacheKey(prefix, string.Join(':', ids));
private static string CombineCacheKey(string prefix, params long[] ids) => CombineCacheKey(prefix, string.Join(':', ids));
They are totally blocked without string.Join<T>
accepting ReadOnlySpan<T>
.
One workaround is merging them as,
private static string CombineCacheKey(string prefix, params ReadOnlySpan<object> ids) => CombineCacheKey(prefix, string.Join(':', ids));
.
However, since the methods are called frequently and most of the inputs are value type, I'm not sure if it could cause boxing each time.
API Proposal
public class String
{
public static string Concat<T>(char separator, ReadOnlySpan<T> values);
public static string Concat<T>(string? separator, ReadOnlySpan<T> values);
public static string Join<T>(char separator, ReadOnlySpan<T> values);
public static string Join<T>(string? separator, ReadOnlySpan<T> values);
}
API Usage
private static string CombineCacheKey(params ReadOnlySpan<int> ids) => string.Join<int>(':', ids);
Alternative Designs
No response
Risks
No response