TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Console Color, Text and BackgroundColor

This C# page shows how to use Console.BackgroundColor and Console.ForegroundColor.

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.

Example. 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

C# program that uses BackgroundColor and ForegroundColor

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();
    }
}

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. 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.

C# program that uses padding and colors

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. 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

C# program that shows all console colors

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. 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.

PadRight, PadLeft

 

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf