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

Console Color Enumeration in C#


C# C# 
Ratings:
0 Comment(s)
 6573  View(s)
Rate this:

Console Color Enumeration in C#

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

1.  class ConsoleColor

2.  {

3.  public static void Main()

4.  {

5.  String nl = Environment.NewLine;

6.  String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));

7.  Console.WriteLine("==================================================================");

8.  Console.WriteLine("Forecolor{0}", nl);

9.  Console.WriteLine("==================================================================");

10. for (int x = 0; x < colorNames.Length; x++)

11. {

12. Console.Write("{0,2}: ", x+1);

13. Console.BackgroundColor = ConsoleColor.Black;

14. Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);

15. Console.Write("This is foreground color {0}.", colorNames[x]);

16. Console.ResetColor();

17. Console.WriteLine();

18. }

19. Console.WriteLine("==================================================================");

20. Console.WriteLine("Backcolor{0}", nl);

21. Console.WriteLine("==================================================================");

22. for (int x = 0; x < colorNames.Length; x++)

23. {

24. Console.Write("{0,2}: ", x);

25. Console.ForegroundColor = ConsoleColor.White;

26. Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);

27. Console.Write("This is background color {0}.", colorNames[x]);

28. Console.ResetColor();

29. Console.WriteLine();

30. }

31. Console.ReadKey();

32. }

33. }

 

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

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

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

13.   Set the background color of console.

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

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

16.    This line of code reset the console color.

Rest of code is performing same task for console background color.

Output: -

Console Color Enumeration in C#

 


Don't want to miss updates? Please click the below button!

Follow MindStick