Skip to content

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

Open
@albert-du

Description

@albert-du

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pri3Indicates issues/PRs that are low priorityarea-System.IOuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions