Skip to content

Consider changing the default console encoding on Windows to UTF-16 #31466

Open

Description

@Serentty

In my tests on Windows, .NET Core's console I/O defaults to the user's local codepage. This leads to quite a bit of overhead as .NET's UTF-16 strings have to be converted to the codepage, just for the console to convert them back to UTF-16. On top of this, it means that a lot of characters can't be properly represented, which I discovered by accident when I was unable to enter a euro sign in my program as my locale (932) doesn't have it. There's an easy fix for this, which is to change the console encoding manually, but unless the programmer is doing fuzzing testing, this problem could be hard to discover.

With that in mind, I propose that the default encoding for console I/O on Windows be changed to UTF-16, allowing low-overhead and lossless passing around of strings between the console and .NET programs.

On Mac and Linux, the default already seems to be UTF-8, which a sensible choice on those platforms, and I think Windows should be brought in line with those other two by using a Unicode encoding (UTF-16) for its I/O as well.

Activity

  1. scalablecory commented on Nov 11, 2019

    @scalablecory
    Contributor

    This seems reasonable. We'd need to understand it from a compat perspective. Most console apps are expecting -- purely by default, so they may not even realize it -- the user's codepage when reading stdin. Changing the default here might create issues when piping between apps.

  2. transferred this issue fromdotnet/coreon Nov 11, 2019
  3. huoyaoyuan commented on Nov 12, 2019

    @huoyaoyuan
    Member

    Shouldn't morden applications be totally agnostic to code pages? All strings should only be processed in some variation of Unicode.

  4. Serentty commented on Nov 12, 2019

    @Serentty
    ContributorAuthor

    @huoyaoyuan I agree. The issue here is deciding whether it's okay to make this change, which is possibly breaking for a very few specific use cases. I think user I/O over the console should be mostly unaffected, but as was brought up, this could break applications which assume codepages when piping the standard input and output. However, such applications are already incredibly fragile since they rely on the user having the correct codepage set system-wide, so they're likely to break all the time even without this change. In my opinion, this change would fix more than it breaks for the vast majority of users.

  5. Serentty commented on Dec 15, 2019

    @Serentty
    ContributorAuthor

    I've seen some other threads complaining that the console is defaulting to UTF-8 instead of codepages for them on Windows, but these are a few years old, so I'm a bit confused now. Was the default behaviour changed to default to codepages instead?

  6. transferred this issue fromdotnet/corefxon Feb 1, 2020
  7. added
    untriaged
    on Feb 23, 2020
  8. Serentty commented on Jun 12, 2020

    @Serentty
    ContributorAuthor

    @scalablecory Would a change like this be a good fit for a major release like .NET 5 or 6? After all, those usually have a list of migration notes, compared to more minor releases.

  9. added this to the Future milestone on Jun 24, 2020
  10. removed
    untriaged
    on Jul 6, 2020
  11. added
    backlog-cleanup-candidate
    on Jan 6, 2025
  12. huoyaoyuan commented on Jan 6, 2025

    @huoyaoyuan
    Member

    Ping to keep this alive. Encoding is quite annoying for non-Latin locales. We do need a good default solution for it.

  13. removed
    backlog-cleanup-candidate
    on Jan 6, 2025
  14. huoyaoyuan commented on Aug 4, 2026

    @huoyaoyuan
    Member

    I learned today while debugging with encoding. On Windows, output encoding is bound to console, which can be inherited through process tree.

    The same program simply displays Console.OutputEncoding.CodePage. When launched from PowerShell Core by dotnet.exe, which explicitly specifies UTF-8 for the process:

    Image

    When launching from Visual Studio, which uses the system default encoding:

    Image

    and if you alter codepage via SetConsoleCP, the setting will preserve through the parent process (Visual Studio) with multiple debug sessions.

    I also learned that when creating child process with CreateNoWindow, it will not inherit the console, and have an "isolated" codepage context.

    Since it can involve parent process, we can't really implicitly do anything here.

    @adamsitnik if you have more insights about how Windows handles console encoding, welcome to correct me.

  15. huoyaoyuan commented on Aug 4, 2026

    @huoyaoyuan
    Member

    Maybe we can improve the documentation here:

    • Console.Input/OutputEncoding can affect more than current process. It affects the console which can attach multiple processes, including the parent process of current one.
    • ProcessStartInfo.StandardInput/OutputEncoding defaults to current console's encoding. It's correct when child process inherits the console (CreateNoWindow = false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions