In one of my recent applications, I needed to change the foreground and the background colors of the system console.
The Console class in C# represents the system console. We can achieve this by setting Console’s foreground and background properties. The ConsoleColor enum represents a value of the console color. Keep in mind, the ConsoleColor supports a limited number of colors only.
The following code snippet returns the current background and the foreground colors of the console.
- ConsoleColor background = Console.BackgroundColor;
- ConsoleColor foreground = Console.ForegroundColor;
The following code snippet sets the foreground and the background colors of the console.
- Console.ForegroundColor = ConsoleColor.White;
- Console.BackgroundColor = ConsoleColor.Red;
After ForegroundColor and BackgroundColor values are set, call Console.Clear() to apply it to the entire background and foreground.
- Console.Clear();
- Console.ResetColor();
Listing 1 is the complete code that loops through all Console colors and changes console’s background and foreground colors.
- using System;
- class ConsoleColorsClass {
- static void Main(string[] args) {
- // Let's go through all Console colors and set them as foreground
- foreach(ConsoleColor color in Enum.GetValues(typeof(ConsoleColor))) {
- Console.ForegroundColor = color;
Console.Clear();
- Console.WriteLine($ "Foreground color set to {color}");
- }
- Console.WriteLine("=====================================");
- Console.ForegroundColor = ConsoleColor.White;
- // Let's go through all Console colors and set them as background
- foreach(ConsoleColor color in Enum.GetValues(typeof(ConsoleColor))) {
- Console.BackgroundColor = color;
- Console.WriteLine($ "Background color set to {color}");
- }
- Console.WriteLine("=====================================");
- // Restore original colors
- Console.ResetColor();
- Console.ReadKey();
- }
- }
The output of Listing 1 generates Figure 1.

Figure 1

Saravanakumar SekaranPosted Jun 26, 2018, 4:59 AM
Wow excellent article !!!!
Faisal PathanPosted Jun 20, 2018, 6:20 AM
I never Expected that we can customization in console like this, Superb Sir :)
Rushi MehtaPosted Jun 18, 2018, 6:11 AM
Fantastic Arcticle
Arun Kumar SinghPosted Jun 18, 2018, 3:25 AM
Wow nice one sir
Bhairab DuttPosted Jun 17, 2018, 12:54 PM
Nice article ....
Hemanth KumarPosted Jun 17, 2018, 12:41 PM
:) Thanks mahesh, never knew such customization s are available for Console. Will play around