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

Activity

dotnet-policy-service

dotnet-policy-service commented on Apr 6, 2024

@dotnet-policy-service
Contributor

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

added a commit that references this issue on Apr 18, 2024
49a5d06
linked a pull request that will close this issue on Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @albert-du

      Issue actions

        StringReader.Read(Span<Char>) does not actually throw IOException · Issue #9798 · dotnet/dotnet-api-docs