.NET 控制台应用程序将某些 Unicode 值显示为问号;这是可以修复的吗?

.NET console app showing certain Unicode value as question mark; is this fixable?

我正在用 C# 编写 .NET 5 控制台应用程序。我希望能够做的一件事是显示两个特定的 Unicode 字符:一颗完整的心( - Unicode 编号 2665)和一颗空心( - Unicode 编号 2661)。因此:

const char emptyHeart = '\u2661';
const char fullHeart = '\u2665';

我假设我已正确设置,但由于某种原因,控制台将 emptyHeart 显示为问号而不是正确的字符。因此,例如,包含三个 fullHeart 后跟四个 emptyHeart 的字符串显示为 ♥♥♥???? 而不是预期的 ♥♥♥♡♡♡♡。这是否可以在不更改 Unicode 字符(例如填充和空心圆圈)的情况下修复?如果是,怎么做?

https://docs.microsoft.com/dotnet/api/system.console.outputencoding?view=net-5.0#notes-to-callers

Note that successfully displaying Unicode characters to the console requires the following:

  • The console must use a TrueType font, such as Lucida Console or Consolas, to display characters.
  • A font used by the console must define the particular glyph or glyphs to be displayed. The console can take advantage of font linking to display glyphs from linked fonts if the base font does not contain a definition for that glyph.

默认的 Windows 控制台字体 Consolas 确实包含 \u2665 的字形,但根据 https://www.fileformat.info/info/unicode/font/consolas/grid.htm.

的 U+2660 行不包含 \u2661 的字形

因此,您唯一的选择是将控制台的字体更改为支持该字形的字体。然而,这不是一个微不足道的操作;请参阅 https://docs.microsoft.com/dotnet/api/system.console#unicode-support-for-the-console 中的“以下示例显示如何以编程方式将字体从光栅字体更改为 Lucida Console”(尽管 Lucida Console 也不支持 \u2661,因此您必须找到一种字体).