Console.Foregroundcolor Property In C#

In this post, we are going to explore the Console.ForegroundColor attribute in C# along with its syntax, arguments, and illustrations.

What is the Console.ForegroundColor Property?

Console.ForegroundColor represents a property within the C# programming language. This particular property is a component of the Console class and is responsible for defining the color that appears as the foreground in the Console. By default, this property is set to grey. Its primary function is to modify the text color displayed in the Console output, thereby altering the appearance of the Console itself. This adjustment not only diversifies the Console's appearance but also enhances the legibility of applications running in the Console. As a result, it aids users in promptly discerning the importance level of individual messages.

This feature will improve the legibility and offer visual indicators regarding the type of data. In certain scenarios, the color red signifies errors, while the color green signifies successful operations, and so forth. This attribute proves beneficial when developing text-based user interfaces.

Syntax:

The syntax of the Console.ForegroundColor in C#:

Example

Console.ForegroundColor = ConsoleColor.ColorName;

This attribute does not require any parameters and its output type is void. It assigns a color value to modify the context text. The colorName specified in the syntax can represent various colors such as Red, Blue, Yellow, and so on.

Example:

Let's consider a basic program to demonstrate the utilization of the Console.ForegroundColor attribute in the C# programming language.

Example

using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("This is some text in the default color.");
        
        // Set foreground color to red
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("This text will be displayed in red.");
        // Set foreground color to Yellow
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("Now this text will be displayed in Yellow.");
        // Set foreground color to Green
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("Now this text will be displayed in Green.");
        // Reset foreground color to default
        Console.ResetColor();
        Console.WriteLine("Back to default color.");
    }
}

Output:

The code snippet below demonstrates the CSS styling for a placeholder diagram:

Example

.placeholder-diagram { background: linear-gradient(135deg, #374151 0%, #1f2937 100%); border-radius: 12px; padding: 40px; margin: 20px 0; text-align: center; }
.placeholder-diagram .placeholder-icon { font-size: 3rem; margin-bottom: 10px; }
.placeholder-diagram .placeholder-text { color: #9ca3af; font-size: 1rem; }

Explanation:

This tutorial demonstrates how to utilize the color-changing feature in C#. At the beginning, the text color of the Console is white. This color can be altered by employing the ForegroundColor property. In this example, the color is adjusted to red initially, then transitions to yellow, and finally to green. To revert the Console back to its default color, the ResetColor property, found within the console class, is used.

Example 2:

Let's consider a straightforward example to showcase the Console.ForegroundColor feature in C#.

Example

using System;
using System.Collections.Generic;
class Program
{
    static void Main()
    {
        Dictionary<string, ConsoleColor> userColors = new Dictionary<string, ConsoleColor>();
        Console.WriteLine("Enter user names and select a color (or 'done' to finish):");
        while (true)
        {
            Console.Write("Enter user name (or 'done' to finish): ");
            string userName = Console.ReadLine().Trim();
            if (userName.ToLower() == "done")
                break;
            ConsoleColor userColor = SelectColor();
            userColors.Add(userName, userColor);
        }
        Console.WriteLine("Welcome to the Console Chat!");
        Console.WriteLine("Enter 'q' to quit.");
        while (true)
        {
            Console.WriteLine("Select user name (or 'q' to quit): ");
            string input = Console.ReadLine().Trim();
            if (input.ToLower() == "q")
                break;
            if (userColors.ContainsKey(input))
            {
                ConsoleColor userColor = userColors[input];
                Console.WriteLine("Enter your message: ");
                string message = Console.ReadLine();
                Console.ForegroundColor = userColor;
                Console.WriteLine($"{input}: {message}");
                Console.ResetColor();
            }
            else
            {
                Console.WriteLine("User not found. Please enter a valid user name.");
            }
        }

        Console.WriteLine("Goodbye!");
    }
    static ConsoleColor SelectColor()
    {
        Console.WriteLine("Available colors:");
        Console.WriteLine("1. Red");
        Console.WriteLine("2. Green");
        Console.WriteLine("3. Blue");
        Console.WriteLine("4. Yellow");
        Console.WriteLine("5. Cyan");
        while (true)
        {
            Console.Write("Enter the number of the color: ");
            if (int.TryParse(Console.ReadLine(), out int colorIndex) && colorIndex >= 1 && colorIndex <= 5)
            {
                ConsoleColor selectedColor = (ConsoleColor)(colorIndex - 1);
                return selectedColor;
            }
            else
            {
                Console.WriteLine("Invalid input. Please enter a number between 1 and 5.");
            }
        }
    }
}

Output:

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 code showcases how to utilize the ForegroundColor attribute. To begin, the program imports essential namespaces. Subsequently, it sets up a dictionary where the keys are strings and their corresponding values are colors. Following that, a while loop is implemented to receive user input, where users provide names along with their associated colors.

Following this, the dictionary is populated with key-value pairs. The while loop will terminate upon the user inputting "done". Subsequently, the program will initialize, prompting the user to input their name and message. The message will be displayed on the Console in the specified color. To exit the loop, the user can input "q". Finally, a farewell message will be printed, signaling the conclusion of the application.

Input Required

This code uses input(). Please provide values below: