dotnet-api-docs icon indicating copy to clipboard operation
dotnet-api-docs copied to clipboard

StringReader.Read(Span<Char>) does not actually throw IOException

Open albert-du opened this issue 1 year ago • 1 comments

Hi,

The doc for StreamReader.Read(Span<Char>) claims that it'll throw an IOException if "the number of characters read from the stream is larger than the buffer length."

This is not true, as seen with this code:

var array = new char[8];

Span<char> span = array;

File.WriteAllText("text.txt", "0123456789");

using StreamReader reader = new("text.txt");

var result = reader.Read(span);

Console.WriteLine(result);
Console.WriteLine(span.ToString());

// 8
// 01234567

StringReader has the same behavior and is documented correctly:


// StringReader
var array = new char[8];
var span = array;

using StringReader textReader = new("0123456789");

var result = textReader.Read(span);

Console.WriteLine(result);
Console.WriteLine(span.ToString());

// 8
// 01234567

If it's welcome, I would be happy to help fix the documentation

albert-du avatar Apr 06 '24 23:04 albert-du

Tagging subscribers to this area: @dotnet/area-system-io