Skip to content

StreamReader.ReadLine fails to return null when end-of-stream is reached #23589

@tintoy

Description

@tintoy

Hi.

I've run into some puzzling behaviour with StreamReader.ReadLine that appears to deviate that described in the documentation. According to these docs, StreamReader.ReadLine should return null if the end of the input stream is reached (before a line-ending sequence is encountered, I assume, although that assumption may be the source of the problem). But that's not the behaviour I'm seeing.

A simple repro:

byte[] data = Encoding.ASCII.GetBytes("Hello world"); // No newline
using (MemoryStream dataStream = new MemoryStream(data))
using (StreamReader dataReader = new StreamReader(dataStream, Encoding.ASCII, detectEncodingFromByteOrderMarks: false, bufferSize: 5))
{
    string line = dataReader.ReadLine();
    if (line == null)
        Console.WriteLine("No line.");
    else
        Console.WriteLine($"Line: '{line}'.");
}

Am I reading the documentation correctly, or is the implication that ReadLine will return whatever it has even if it has never encountered a new-line sequence?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions