Skip to content

[API Proposal]: Implement IAsyncDisposable on TextReader #88244

@Neme12

Description

@Neme12

Background and motivation

By default, when disposing a StreamReader or StreamWriter, the underlying Stream itself is disposed. Since Stream supports async disposal via IAsyncDisposable, both StreamReader and StreamWriter should implement IAsyncDisposable as well to support the case when the underlying stream supports it. Currently, only StreamWriter implements IAsyncDisposable while StreamReader doesn't, so when using StreamReader, it's only possible to dispose the underlying stream synchronously.

API Proposal

namespace System.IO;

// new
public abstract partial class TextReader : MarshalByRefObject, IDisposable, IAsyncDisposable
{
    public virtual ValueTask DisposeAsync();
}

// existing
public abstract partial class TextWriter : MarshalByRefObject, IDisposable, IAsyncDisposable
{
    public virtual ValueTask DisposeAsync();
}

// new
public partial class StreamReader : TextReader
{
    public override ValueTask DisposeAsync();
}

// existing
public partial class StreamWriter : TextWriter
{
    public override ValueTask DisposeAsync();
}

API Usage

await using var reader = new StreamReader(myAsyncStream);

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.IO

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions