Its not a good idea to use Console colors too much

With Whidbey (VS2005) managed Console based applications have got a boost with new additions to the System.Console namespace. You can now change the color, size, buffer-size, cursor-size, cursor-position, window-title directly from a managed application without PInvoke.

However, just because it can be done easily, doesn't mean that it should be used. If you are developing a console based game or something similar like a chat client you have the liberty to do this. Otherwise think twice (or thrice) before you play around with any of these settings. The reason is simple most people (like me) will get super annoyed if for some reason the console window suddenly jumps and resizes when I run a program.

If you think that doing some thing simpler like changing the text color (using Console.ForegroundColor) to draw attention is Ok, consider it carefully. It might just not work, could look horrible on some specific console setting or might convey the wrong meaning. I have listed below some common usage of changing these setting programmatically and why they might not work. Most programmers agree and understand that playing with console window size or buffer-size is not a good idea and do not use it so I have omitted them from here.

Changing text color using Console.ForegroundColor

Warning and Error Messages: Using different colors for output text, like yellow for warnings and red for error might be fine. You just need to ensure that you use the right set of colors as their meaning are deeply bound in the mind of the users. The following table typically works

  • Yellow: warning
  • Red: Error
  • Green: Pass, success messages
  • Cyan: Information

Do NOT use colors when its not absolutely required: Its best to avoid using colors when not absolutely required. In most cases it'll lead to trouble and rarely be of any benefit. See the following example where the prompt comes in yellow. Since this is a console based application it would have blocked until I entered the required inputs so drawing attention with yellow does not add any value. Since yellow is associated with warning, a first-time user might thing that something is wrong and he's required to rectify this by entering some server name.

 Welcome to my tool
Server  : MyServer
Username: Abhinaba

Do NOT use a dark color for text: People do change the background color of the console. I use Teal and DarkBlue often, so ensure that you do not choose one of the darker colors for the text as it might coincide with the background color of the console window and your text will be invisible. I once came accross a tool which used blue for text entered by the user. I had launched that application on a blue-background console. Since I was using the application for the first time, I had a hard time figuring out what was going on, as I couldn't see what I was typing.

The safe colors are Gray, Green, Cyan, Red, Magenta, Yellow and White. However sometimes even these colors in combination with background colors cause eye-sore as in

 Some error message!!

Do restore the original color: Even if you use any of the above colors remember to switch the color back to the original color. Use some thing like

 public void ShowError(string message)
    ConsoleColor orgCol = Console.ForegroundColor; 
    Console.ForegroundColor= ConsoleColor.Red;
    Console.ForegroundColor = orgCol;  // restore color

This might look trivial, but can be a big source of annoyance. Like your application gets an exception, the exception-handler sets color to red, shows the error message and the application exits. Now whatever I type in that console window is in Red!!!

Using Console.BackgroundColor

Do NOT change the background color: Console.BackgroundColor = ... is bad.  The reason being that the color is changed only for subsequent text and it looks horrible as below

 Text before color change
Text after I changed the background col to cyan

Even though very few people intentionally do this, it does creep into application. Sometime back in some error message code I saw this.

 Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;

This went undetected in test, until someone opened it in a Teal window and this is what came up

 Some error message!!

Playing with cursor

CursorVisible: You might want to hide the cursor when showing some output using Console.CursorVisible = false. Just remember to revert it back to the same visibility state as it originally was.

CursorPosition: Jumping the cursor around in the screen is not a main-stream scenario and most console application don't use it. However, I have seen some tools like build-systems use this to show subsquent outputs in the same line. Consider the following code

 int currPos = Console.CursorTop; // get the current pos 
for (int i = 0; i < 30; i++) 
    Console.WriteLine("Counting {0}", i); 
    Console.CursorTop = currPos; // Go back to the same line

This shows the counter in the same line. This might be required in your application. However, in some cases its over-done and you can simply live without it...

Most programmers are used to UI guidelines. Unfortunately most of these guidelines ingnore command line interface and are silent on them...