C#

Is it possible to write to the console in colour in NET

25 September 2026 · 4 min read

Is it possible to write to the console in colour in NET

Adding color to your .NET console applications can dramatically improve their readability and user experience. Whether you’re building a command-line tool, a debugging utility, or simply want to make your output more engaging, colored console text can make a world of difference. But how exactly do you achieve this in .NET? This article explores the various methods for writing colored text to the console in .NET, from simple built-in methods to more advanced techniques, providing you with the knowledge to create visually appealing and informative console applications.

Simple Color Output with Console.ForegroundColor

The simplest way to add color to your console output is using the Console.ForegroundColor property. This property allows you to change the color of the text that is subsequently written to the console. You can set it to one of the predefined console colors enumerated in the ConsoleColor enum, such as ConsoleColor.Red, ConsoleColor.Green, or ConsoleColor.Blue.

For instance, to write “Hello in red!” to the console, you would use the following code:

Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Hello in red!"); Console.ResetColor(); // Important: Resets the color back to default 

Remember to reset the color using Console.ResetColor() after writing the colored text to prevent subsequent output from also being colored.

Advanced Color Control with ANSI Escape Codes

While Console.ForegroundColor provides basic color functionality, ANSI escape codes offer greater control and flexibility. ANSI codes are special sequences of characters that can be embedded in text to control formatting, including color, cursor position, and more. They are widely supported across different operating systems and terminal emulators.

Using ANSI codes, you can specify not only the foreground color but also the background color. For example, to write “Hello in green on a blue background!” you could use:

Console.WriteLine("\u001b[32m\u001b[44mHello in green on a blue background!\u001b[0m"); 

The \u001b[ sequence initiates an ANSI escape code. The codes 32m (green foreground), 44m (blue background), and 0m (reset formatting) control the styling. This approach allows for a wider range of color combinations than Console.ForegroundColor.

Cross-Platform Considerations

While ANSI escape codes are generally well-supported, some older Windows consoles might not handle them correctly. To ensure cross-platform compatibility, you can check if the console supports ANSI codes before using them:

if (Console.IsOutputRedirected || !Environment.OSVersion.Platform.Equals(PlatformID.Win32NT)) { // Use ANSI codes } else { // Fallback to Console.ForegroundColor } 

This check ensures that your application works correctly regardless of the target environment.

Building Reusable Color Functions

To streamline your code and avoid repetitive color setting, consider creating helper functions:

public static void WriteColor(string text, ConsoleColor foreground, ConsoleColor background = ConsoleColor.Black) { // Implementation using Console.ForegroundColor or ANSI codes based on platform } 

This function encapsulates the color logic and simplifies its usage throughout your application. For example, you could call WriteColor("Success!", ConsoleColor.Green) to easily print a green success message.

  • Using color enhances readability and highlights important information.
  • ANSI escape codes provide fine-grained control over color and formatting.
  1. Choose the color method based on your project’s needs and target platform.
  2. Remember to reset the color after writing colored text.
  3. Encapsulate color logic in reusable functions for cleaner code.

For further information on console output in .NET, see the official documentation: System.Console Class

Color in the console can drastically improve the user experience, especially in command-line applications. By following the techniques outlined in this article, you can create visually appealing and informative console output that is both functional and engaging.

[Infographic about Console Colors and Codes]

Frequently Asked Questions (FAQ)

Q: What if my console doesn’t support ANSI escape codes?

A: Fall back to using the Console.ForegroundColor property, which provides basic color support on all platforms. You can detect ANSI support using Console.IsOutputRedirected and Environment.OSVersion.Platform.

Coloring console output is a powerful technique for improving the user experience of your .NET command-line applications. Whether using simple built-in methods or more advanced ANSI escape codes, choosing the right approach depends on your project’s specific needs and platform compatibility requirements. Explore the resources mentioned in this article and experiment with different techniques to find the best solution for creating visually engaging and informative console output. Start enhancing your console applications today with the power of color! Learn more about advanced console techniques. For other perspectives, check out ColoredConsole, Command Line Parser Library and Spectre.Console.

  • Console Coloring
  • ANSI Escape Codes
  • Cross-Platform Compatibility
  • .NET Console Applications
  • User Interface Design
  • Command-line Tools
  • Debugging Utilities

Question & Answer :
Writing a small command line tool, it would be nice to output in different colours. Is this possible?

Yes. See this article. Here’s an example from there:

Console.BackgroundColor = ConsoleColor.Blue; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("White on blue."); 

enter image description here