Runtime: Should ReadOnlySpan<char>.ToString() return the contents of the span?

Created on 5 Jul 2017  路  6Comments  路  Source: dotnet/runtime

Trying out the new Spans for the first time and I was expecting ReadOnlySpan<char>.ToString() to be an easy way to do a conversion. It just returns the default type name.

I understand that ReadOnlySpan<T> is an array-like structure but with https://github.com/dotnet/corefx/issues/21395 coming ReadOnlySpan<char> is going to look string-like.

Is their any precedent to Something<char>.ToString() being special cased?

area-System.Memory question

Most helpful comment

Span<char> is very much like array. char[].ToString() does not return the content as a string either.

All 6 comments

Span<T> is a too low level type to override ToString. There could be an extension method for ReadOnlySpan<char> but more practical approach would be to add another ctor tostring which will be taking ReadOnlySpan<char>

Handled by something around System.Text.Primitives?

Though not sure if outputing a string is in there? /cc @KrzysztofCwalina

@omariom I think it's a good idea to add the string ctor either way but to me the ToString() looks better. Closer to a StringBuilder than an array.

string a = myCharSpan.Trim().ToString();

string b = new String(myCharSpan.Trim());

It is not critical but a nice to have.

Don't think you can specifically override; or provide an extension method (as is defined instance method) for

Span<char>.ToString()

Would have to override Span<T>.ToString() then typeof(T) to get that effect

Span<char> is very much like array. char[].ToString() does not return the content as a string either.

Fair call 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nalywa picture nalywa  路  3Comments

jamesqo picture jamesqo  路  3Comments

omajid picture omajid  路  3Comments

matty-hall picture matty-hall  路  3Comments

jzabroski picture jzabroski  路  3Comments