C# Reference ยท Files and I/O
Console colors and cursor control
Reference for console colors in C#: ForegroundColor, BackgroundColor, ResetColor, ANSI escape codes, SetCursorPosition, Clear and redirected output.
A console program can change how its text looks. Console.ForegroundColor and Console.BackgroundColor take a ConsoleColor value (Red, DarkGreen, Yellow, ...) and apply to everything written afterwards, until you call Console.ResetColor(). Cursor members such as SetCursorPosition, CursorVisible, Clear() and WindowWidth let you build simple full-screen layouts and progress displays.
These features exist only for an interactive terminal. When output is redirected to a file, a pipe or a test runner, there is no screen: the color properties keep their values but nothing is emitted, WindowWidth is 0, and cursor movement does nothing useful. Well-behaved programs check Console.IsOutputRedirected before decorating their output. The modern alternative to the color properties is writing ANSI escape sequences, which most terminals (including Windows Terminal) understand.
Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("ok"); Console.ResetColor();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static ConsoleColor ForegroundColor { get; set; } public static ConsoleColor BackgroundColor { get; set; } public static void ResetColor(); public static void SetCursorPosition(int left, int top); public static (int Left, int Top) GetCursorPosition(); public static bool CursorVisible { get; set; } public static int WindowWidth { get; set; } public static int WindowHeight { get; set; } public static void Clear(); public static string Title { get; set; } public static void Beep();
| Member | Description |
|---|---|
ForegroundColor, BackgroundColor |
Current text and background colors. The 16 values are Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White. |
ResetColor() |
Returns both colors to the terminal's defaults. |
SetCursorPosition(left, top) |
Moves the cursor; both values are zero-based and must lie inside the buffer. |
GetCursorPosition() |
Current column and row as a tuple (.NET 6 and later). |
Clear() |
Clears the screen and homes the cursor. |
WindowWidth, WindowHeight |
Size of the visible area in characters. Setting them is supported on Windows only. |
Title, Beep() |
Window title and the system beep. |
Errors: ArgumentOutOfRangeException for cursor positions outside the buffer, IOException when a member needs a real console and none exists (the exact members that throw depend on the platform and on whether output is redirected), PlatformNotSupportedException for members that exist on Windows only.
Usage notes
- Reset what you set. Colors are process-wide terminal state. If your program exits while red text is active, the user's shell prompt stays red. Use
try/finallyor a small helper, as in the example. - Do not assume a terminal. When piped or redirected,
WindowWidthis0and cursor movement is meaningless. Guard withConsole.IsOutputRedirectedand fall back to plain text. Treat a width of0as "unknown" and use a default such as80. - Respect
NO_COLOR. Many users set theNO_COLORenvironment variable to ask for plain output; check it before emitting escape codes. - ANSI vs. ConsoleColor.
ConsoleColorhas 16 colors but works everywhere and is cleared automatically on redirect. ANSI sequences allow 256 colors, true color, bold and underline, but are written verbatim into redirected output unless you check first. - Thread safety. Color changes are global, so two threads printing in different colors will interleave. Lock around "set color, write, reset", or build a single string with ANSI codes and write it in one call.
- Progress bars. Write
\r(carriage return) to rewind to the start of the line, or useConsole.SetCursorPosition(0, row). Pad with spaces to erase a longer previous message. - Windows legacy consoles. Very old
conhostwindows needed virtual terminal processing to be enabled for ANSI; current Windows Terminal and recent Windows 10/11 consoles handle it by default. - Libraries. For tables, prompts, spinners and markup, a library such as Spectre.Console builds on these primitives.