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.
The following code snippet sets the foreground and the background colors of the console.
After ForegroundColor and BackgroundColor values are set, call Console.Clear() to apply it to the entire background and foreground.
Listing 1 is the complete code that loops through all Console colors and changes console’s background and foreground colors.
The output of Listing 1 generates Figure 1.