Skip to content

Clarify Encoding.GetEncoding(0) and Encoding.Default behavior differences between .NET Framework and .NET Core+ #11712

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

Merged
merged 3 commits into from
Aug 19, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 19, 2025

This PR clarifies the documentation for Encoding.GetEncoding(0) and Encoding.Default to explain their different behaviors between .NET Framework and .NET Core/later versions.

Background

In .NET Framework, both Encoding.Default and Encoding.GetEncoding(0) return the system's active code page encoding. However, in .NET Core and later versions, this behavior changed:

  • Encoding.Default always returns UTF-8
  • Encoding.GetEncoding(0) behavior depends on registered encoding providers

This difference can cause confusion for developers migrating code between frameworks.

Changes Made

Enhanced Encoding.Default Documentation

Added comprehensive remarks explaining that:

  • In .NET Framework: Returns system's active code page (same as GetEncoding(0))
  • In .NET Core+: Always returns UTF-8
  • Recommends using specific Unicode encodings for cross-platform consistency

Clarified Encoding.GetEncoding(int) Behavior for codepage = 0

Updated documentation to explain platform and provider-specific behavior:

  • No provider registered: Returns UTF-8 (same as Encoding.Default)
  • CodePagesEncodingProvider registered: Windows returns active code page, non-Windows returns UTF-8
  • Custom provider registered: Behavior determined by that provider

Updated Encoding.RegisterProvider Documentation

Enhanced explanation of how registering encoding providers affects GetEncoding(0) behavior, with specific details about CodePagesEncodingProvider's platform-specific impact.

Improved CodePagesEncodingProvider Documentation

  • Added new section explaining impact on default encoding behavior
  • Enhanced GetEncoding(int) method documentation with specific codepage = 0 behavior
  • Replaced placeholder "To be added" remarks with comprehensive documentation

These changes help developers understand the encoding behavior differences and make informed decisions when writing cross-platform code.

Fixes #11711.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…ehavior differences

Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Clarify the Encoding.GetEncoding(0) and Encoding.Default behavior Clarify Encoding.GetEncoding(0) and Encoding.Default behavior differences between .NET Framework and .NET Core+ Aug 19, 2025
@Copilot Copilot AI requested a review from danmoseley August 19, 2025 20:45
Copilot finished work on behalf of danmoseley August 19, 2025 20:45
Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gewarren could you please have a quick look before we merge this one?

@tarekgh tarekgh marked this pull request as ready for review August 19, 2025 22:32
@tarekgh tarekgh requested a review from a team as a code owner August 19, 2025 22:32
Copy link
Contributor

@gewarren gewarren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some suggestions for consideration.

Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
@tarekgh tarekgh merged commit 3391a13 into main Aug 19, 2025
5 checks passed
@tarekgh tarekgh deleted the copilot/fix-11711 branch August 19, 2025 23:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clarify the Encoding.GetEncoding(0) and Encoding.Default behavior
4 participants