dotnet / dotnet/dotnet-api-docs

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

Open
#9,798 1 comment 0 reactions 0 assignees View on GitHub
area-System.IO Pri3 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

Hi,

The doc for `StreamReader.Read(Span)` claims that it'll throw an IOException if ["the number of characters read from the stream is larger than the buffer length."](
https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader.read?view=net-8.0#system-io-streamreader-read(system-span((system-char))))

This is not true, as seen with this code:
```csharp
var array = new char[8];

Span 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](https://learn.microsoft.com/en-us/dotnet/api/system.io.stringreader.read?view=net-8.0#system-io-stringreader-read(system-span((system-char)))) has the same behavior and is documented correctly:

```csharp

// 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.