Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamWriter does use TextWriter.NewLine for newlines rendering overriding get only useless #111531

Open
WBMPH opened this issue Jan 17, 2025 · 1 comment
Labels
area-System.IO untriaged New issue has not been triaged by the area owner

Comments

@WBMPH
Copy link

WBMPH commented Jan 17, 2025

Description

When override get property TextWriter.NewLine, it does not affect the newline being used as a set on the property is required in order to set the backing field that is actually used. This is quite unintuitive as one would expect the overloaded get NewLine to be used, otherwise what is the point of it being overridable anyways?

Reproduction Steps

public sealed class SmileyStreamWriter(Stream stream, IFormatProvider formatProvider, bool leaveOpen)
    : StreamWriter(stream, System.Text.Encoding.UTF8, -1, leaveOpen)
{
    public override string NewLine => ":)";

    public override IFormatProvider FormatProvider { get; } = formatProvider;
}

using var file = File.Open("test.txt", FileMode.CreateNew);
var sw = new SmileyStreamWriter(file, CultureInfo.InvariantCulture, leaveOpen: true)
sw.WriteLine("test"); // Writes test\r\n (or \n on unix). 

Expected behavior

Expect to use the get override of newline.

Actual behavior

Uses platform default.

Regression?

No idea.

Known Workarounds

Use explicit NewLine set in constructor of derived class rendering overriding pointless.

Configuration

No response

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 17, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.IO untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

1 participant