articles

Home / DeveloperSection / Articles / Console Color Enumeration in C#

Console Color Enumeration in C#

Console Color Enumeration in C#

Vijay Shukla 20196 23-Jun-2013

In this article I am trying to explain the concept of Console Color Enumeration in C#.

Console Color

We can be change the text and background colors. This sometimes makes programs easier to use. We change colors to make programs more easy-to-read. Errors and alerts are more noticeable. This leads to higher quality. Console Color Enumeration specified constants that define foreground and background colors for the console.

Color member name
Member name
Description

Black

The color black.

Blue

The color blue.

Cyan

The color cyan (blue-green).

DarkBlue

The color dark blue.

DarkCyan

The color dark cyan (dark blue-green).

DarkGray

The color dark gray.

DarkGreen

The color dark green.

DarkMagenta

The color dark magenta (dark purplish-red).

DarkRed

The color dark red.

DarkYellow

The color dark yellow (ochre).

Gray

The color gray.

Green

The color green.

Magenta

The color magenta (purplish-red).

Red

The color red.

White

The color white.

Yellow

The color yellow.

Console Color Enumeration Example:-
 class ConsoleColor

 {
 public static void Main()
 {
 String nl = Environment.NewLine;
 String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));
 Console.WriteLine("==================================================================");
 Console.WriteLine("Forecolor{0}", nl);
 Console.WriteLine("==================================================================");
 for (int x = 0; x < colorNames.Length; x++)
 {
 Console.Write("{0,2}: ", x+1);
 Console.BackgroundColor = ConsoleColor.Black;
 Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);
 Console.Write("This is foreground color {0}.", colorNames[x]);
 Console.ResetColor();
 Console.WriteLine();
 }
 Console.WriteLine("==================================================================");
 Console.WriteLine("Backcolor{0}", nl);
 Console.WriteLine("==================================================================");
 for (int x = 0; x < colorNames.Length; x++)
 {
 Console.Write("{0,2}: ", x);
 Console.ForegroundColor = ConsoleColor.White;
 Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);
 Console.Write("This is background color {0}.", colorNames[x]);
 Console.ResetColor();
 Console.WriteLine();
 }
 Console.ReadKey();
 }
 }

Code Description:-

1.       Start the class.

3.       Main method start

5.       Create a string type nl variable which is save new line constant in the form of "/r/n".

6.       Create a string type array with colorNames name and in this array initialize the color names for the console.

7.       to 9. Give the user a friendly message on the console screen.

10.   This is for loop and this loop run colorNames.Length count.

13.   Set the background color of the console.

14.   Set the four-color of console text from Color Enumeration.

15.   This line of code gives the message on the console in which color is set on text fore-color.

16.    This line of code resets the console color.

The rest of the code is performing same task for console background color.

Output: -

Console Color Enumeration in C#


You should read this Article - Bootstrap Color Picker


Updated 07-Feb-2020

Leave Comment

Comments

Liked By