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 constructor documentation mismatch with implementation #106237

Closed
rameel opened this issue Aug 11, 2024 · 3 comments · Fixed by #106658
Closed

StreamWriter constructor documentation mismatch with implementation #106237

rameel opened this issue Aug 11, 2024 · 3 comments · Fixed by #106658
Labels
area-System.IO help wanted [up-for-grabs] Good issue for external contributors in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@rameel
Copy link

rameel commented Aug 11, 2024

There is an inconsistency between the documentation and the actual implementation of the StreamWriter constructors. The documentation states that passing null to the Encoding parameter in some constructors, except the main one, will result in an exception being thrown. However, all these constructors pass the Encoding argument to the main constructor, which allows null.

public StreamWriter(Stream stream, Encoding encoding)
    : this(stream, encoding, DefaultBufferSize, false);

public StreamWriter(Stream stream, Encoding encoding, int bufferSize)
    : this(stream, encoding, bufferSize, false);
public StreamWriter(Stream stream, Encoding? encoding = null, int bufferSize = -1, bool leaveOpen = false);

StreamWriter.cs
Documentation

Suggested Fix:

  • Mark the Encoding parameter as nullable in these constructors.
  • Update the documentation.
- public StreamWriter(Stream stream, Encoding encoding);
- public StreamWriter(Stream stream, Encoding encoding, int bufferSize);
+ public StreamWriter(Stream stream, Encoding? encoding);
+ public StreamWriter(Stream stream, Encoding? encoding, int bufferSize);
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Aug 11, 2024
Copy link
Contributor

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

@DaZombieKiller
Copy link
Contributor

According to the reference sources, all of the constructors appear to throw for a null encoding value in .NET Framework. I suppose this might just be a case where the behavior changed in .NET Core and some of the overloads got missed when updating the docs.

@jozkee jozkee added this to the Future milestone Aug 12, 2024
@jozkee jozkee added help wanted [up-for-grabs] Good issue for external contributors and removed untriaged New issue has not been triaged by the area owner labels Aug 12, 2024
@KalleOlaviNiemitalo
Copy link

The behavior was changed in dotnet/coreclr#24056 to fix #17157. Before that, the StreamReader and StreamWriter constructors would have thrown ArgumentNullException if encoding == null. #2376 is also related.

@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.IO help wanted [up-for-grabs] Good issue for external contributors in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants