C# Console Colors

On a console, text and background colors can be changed. This sometimes makes programs easier to use. We change colors to make programs more expressive. Errors and alerts are more noticeable. This leads to higher quality.

Console coloring in Windows console program

Example

Program

First, the Console has many static methods and properties on it.
To see them,
type "Console"
and press the period. IntelliSense will show you the possible methods.
Here we see BackgroundColor,
ForegroundColor
and ResetColor.

Static MethodConsole.WriteLine
Program that uses BackgroundColor and ForegroundColor: C#

using System; // <-- You need this for Console

class Program
{
    static void Main()
    {
	//
	// 1. Type "Console" and press "."
	// 2. Select "BackgroundColor".
	// 3. Press space and "=", then press tab.
	//
	Console.BackgroundColor = ConsoleColor.Blue;
	Console.ForegroundColor = ConsoleColor.White;
	Console.WriteLine("White on blue.");
	Console.WriteLine("Another line."); // <-- This line is still white on blue.
	Console.ResetColor();
    }
}
Steps

This program includes the System namespace, which contains the Console class. In Main, the BackgroundColor property is set to ConsoleColor.Blue. Next, ForegroundColor is set to ConsoleColor.White.

Property

Note:The two lines, when they are printed, will both have blue backgrounds and white foregrounds.

Also:We see the ResetColor method on Console being used. This sets the colors in your Console back to their defaults.

Example 2

Method call

Often, writing an entire row of color in the Console may be helpful, either to separate output or indicate an important message. Here we refactor the Console code into a separate method.

Tip:In that method, you can change the colors, pad the string, and reset the console.

Program that uses padding and colors: C#

using System;

class Program
{
    static void Main()
    {
	//
	// Write one green line.
	//
	WriteFullLine("This line is green.");
	Console.WriteLine();
	//
	// Write another green line.
	//
	WriteFullLine("This line is also green.");
	Console.WriteLine();
    }

    static void WriteFullLine(string value)
    {
	//
	// This method writes an entire line to the console with the string.
	//
	Console.BackgroundColor = ConsoleColor.Green;
	Console.ForegroundColor = ConsoleColor.DarkGreen;
	Console.WriteLine(value.PadRight(Console.WindowWidth - 1)); // <-- see note
	//
	// Reset the color.
	//
	Console.ResetColor();
    }
}

In the line of the example that uses PadRight, the parameter is the Console.WindowWidth property minus one. This returns a string that will fill up the Console's width. If you do not subtract one, it sometimes incorrectly wraps lines.

Example 3

Color type

Here, we see an original program that displays all the backgrounds and foregrounds possible with the Console. It would be interesting to nest the loops and run though all combinations. MSDN shows a similar program with more complexity.

Console.BackgroundColor Property: MSDN
Program that shows all console colors: C#

using System;

class Program
{
    static void Main()
    {
	//
	// This program demonstrates all colors and backgrounds.
	//
	Type type = typeof(ConsoleColor);
	Console.ForegroundColor = ConsoleColor.White;
	foreach (var name in Enum.GetNames(type))
	{
	    Console.BackgroundColor = (ConsoleColor)Enum.Parse(type, name);
	    Console.WriteLine(name);
	}
	Console.BackgroundColor = ConsoleColor.Black;
	foreach (var name in Enum.GetNames(type))
	{
	    Console.ForegroundColor = (ConsoleColor)Enum.Parse(type, name);
	    Console.WriteLine(name);
	}
    }
}

Output

Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White
Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White

If you run the program above, it will output this list of ConsoleColor values. You can see some of the possible values. If you choose white on blue, you may evoke memories of the BSOD (Blue Screen of Death).

Artists

Drawing: Starry Night

If you are not a visual artist, you should probably be careful about the colors you choose. Consoles are never beautiful, but choosing red on green won't help anyone get their work done.

Instead:I suggest focusing on the structure of your output, with padding. More information on padding is available.

PadLeftPadRight

Summary

We used Console colors,
including BackgroundColor
and ForegroundColor. This article gave me some new ideas for my console programs. The ResetColors method, and the Console.WindowWidth property were also used.


C#: .NET: Console