Mono C# 奇怪的控制台颜色

Mono C# strange console color

我有这个Console艺术功能,

public static void Ask(string message)
{
    ConsoleColor previousColor = Console.ForegroundColor;
    Console.ForegroundColor = ConsoleColor.White;
    Console.Write (message);
    Console.Write (" : ");
    Console.ForegroundColor = previousColor;
}

这是我的Main()

Console.WriteLine("Hello World");
Ask("Roll No");

两个white打印的颜色不一样,如下图

在调试器中,我可以看到 previousColor 也是 ConsoleColor.White

这是一个偶尔会引起混淆的领域。称之为限制而不是错误。

有两个相关因素:

  1. 大多数在 X 中实现颜色的终端都是这样做的,就像 xterm 和 rxvt 自 1990 年代中期以来所做的那样:终端 最初 可以有 default 前景色和背景色已指定,ANSI(和扩展)颜色是后来添加的。与 Linux 控制台不同,无法保证 defaultANSI 颜色相关。

    VTE(终端的功能部分)和 Konsole 一样遵循了该设计。

    引用 xterm 的 manualreverseVideo 的描述表明 ANSI 颜色和终端使用的默认颜色之间存在区别:

               Other control sequences can alter the foreground and background
               colors which are used:

               o   Programs can also use the ANSI color control  sequences  to
                   set the foreground and background colors.

               o   Extensions  to the ANSI color controls (such as 16-, 88- or
                   256-colors) are treated similarly to the ANSI control.

               o   Using other control sequences (the  "dynamic  colors"  fea-
                   ture),  a  program can change the foreground and background
                   colors.
  1. Mono Console 是为了模仿(在某些部分,改编成块的)ncurses 而编写的。 ncurses FAQ Ncurses resets my colors to white/black 指出 ncurses 假定默认颜色为黑底白字 — Console 也遵循该设计。

    它本可以进一步遵循 ncurses 以更加清楚通过告诉终端绘制白色文本完成的显式 "white" 与隐式 "white"(通过重置颜色)之间的区别为默认值)。