Javatpoint Logo
Javatpoint Logo

Console.ForegroundColor Property in C#

In this article, we will discuss the Console.ForegroundColor property in C# with its syntax, parameters, and examples.

What is the Console.ForegroundColor Property?

Console.ForegroundColor is a property in C#. It is part of the Console class and specifies the Console's foreground color. The default color of this property is grey. This property is also used to change the color of the output text in the Console. It makes the Console look different and increases the readability of console applications. It helps users quickly identify the severity of each message.

This property will enhance the readability and provide visual cues about the nature of the information. In some applications, the red color is used to indicate the errors, the green color is used to indicate the success, etc. This property is useful in creating console-based user interfaces.

Syntax:

The syntax of the Console.ForegroundColor in C#:

This property needs no arguments, and its return type is void. A color value is given to this property so that the output context text can be changed. The colorName in the syntax can be any color, some of them are Red, Blue, Yellow etc.

Example:

Let us take a simple program to illustrate the Console.ForegroundColor property in C#.

Output:

Console.ForegroundColor Property in C#

Explanation:

This program explains the usage of the colour-changing property in C#. Initially, the Console's text is white. So, it can be changed by using the ForegroundColor. Here, the color is set to red. After that, it changes to yellow, and then to green. Finally, the Console is set back to its default color by using the ResetColor property, which is present in the console class.

Example 2:

Let us take a simple program to demonstrate the Console.ForegroundColor in C#.

Output:

Enter user names and select a color (or 'done' to finish):
Enter user name (or 'done' to finish): ramu
Available colors:
1. Red
2. Green
3. Blue
4. Yellow
5. Cyan
Enter the number of the color: 2
Enter user name (or 'done' to finish): sita
Available colors:
1. Red
2. Green
3. Blue
4. Yellow
5. Cyan
Enter the number of the color: red
Enter user name (or 'done' to finish): done
Welcome to the Console Chat!
Enter 'q' to quit.
Select user name (or 'q' to quit): 
ramu
Enter your message: 
Hi Sita, have you heard about the latest advancements in Artificial Intelligence (AI) and Machine Learning (ML)? It's fascinating how these technologies are transforming various industries!
ramu: Hi Sita, have you heard about the latest advancements in Artificial Intelligence (AI) and Machine Learning (ML)? It's fascinating how these technologies are transforming various industries!
Select user name (or 'q' to quit): 
sita
Enter your message: 
Hi Ramu! Yes, I've been reading about AI and ML lately. It's incredible how they're revolutionizing everything from healthcare to finance. I'm particularly interested in their applications in predictive analytics and natural language processing.
sita: Hi Ramu! Yes, I've been reading about AI and ML lately. It's incredible how they're revolutionizing everything from healthcare to finance. I'm particularly interested in their applications in predictive analytics and natural language processing.
Select user name (or 'q' to quit):
q
Goodbye!

Explanation:

This program demonstrates the usage of the ForegroundColor property. Firstly, necessary namespaces are imported in the program. After that, a dictionary whose key is the string and the colour's value is initialized. Next, a while loop is used to take the input from the user. Here, the user will give some names and their corresponding color.

After that, the key value pairs are added to the dictionary. The while loop will break if the user enters the "done". Now, the application will start, and the user will enter the user's name and message. This message is printed to the Console by using that color. This while loop will stop giving "q" as input. Lastly, the Goodbye message is printed, which indicates the application has ended.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA