You are working on a Console application using the C# language and want to change the colors of the text and background to make it easier to use. Changing colors on Console can make your program more expressive, so it can communicate errors or other conditions.

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 the BackgroundColor, ForegroundColor, and ResetColor methods, along with the standard 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();
}
}
What the example does. This program includes the System namespace, which contains the Console class. In the Main entry point, the BackgroundColor property is set to ConsoleColor.Blue. Next, the ForegroundColor is set to ConsoleColor.White. The two lines, when they are printed, will both have blue backgrounds and white foregrounds.
ResetColor. In the first example, you can see the ResetColor() method on Console being used. This sets the colors in your Console back to their defaults, which will probably be gray on black.
Often, writing an entire row of color in the Console may be helpful, either to separate output or indicate an important message. Here we see how you can refactor the Console code into a separate method. 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();
}
}Note on using PadRight. In the line of the example that uses PadRight, the parameter is the Console.WindowWidth property minus 1. This returns a string that will fill up the Console's width. If you do not subtract 1, the console can sometimes wrap the lines incorrectly.
PadRight Aligns Strings
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, but this program is simpler. MSDN shows a similar program with more complexity.
MSDN referenceProgram 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);
}
}
}Listing of ConsoleColor values. 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 invoke memories of the BSOD (Blue Screen of Death).
Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White

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 on focusing on the structure of your output, with padding.

Here we saw how to use Console colors, using BackgroundColor and ForegroundColor in the C# language. This article gave me some new ideas for my console programs. The ResetColors method, and the Console.WindowWidth property were also put to good use.
Console Programs