Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to change the Foreground Color of Text in C# Console?
To change the foreground color of text in C# console applications, use the Console.ForegroundColor property. This property accepts a ConsoleColor enumeration value and affects all subsequent text output until changed again.
Syntax
Following is the syntax for setting console foreground color −
Console.ForegroundColor = ConsoleColor.ColorName;
To reset to default colors, use −
Console.ResetColor();
Available Colors
The ConsoleColor enumeration provides the following color options −
| Color Name | Description |
|---|---|
| Black, DarkBlue, DarkGreen, DarkCyan | Dark color variants |
| DarkRed, DarkMagenta, DarkYellow, Gray | Medium intensity colors |
| DarkGray, Blue, Green, Cyan | Standard bright colors |
| Red, Magenta, Yellow, White | High intensity colors |
Example
Let us see an example demonstrating foreground and background color changes −
using System;
class Demo {
public static void Main(string[] args) {
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("Background color changed = " + Console.BackgroundColor);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\nForeground color changed = " + Console.ForegroundColor);
}
}
The output of the above code displays text with blue background and yellow foreground colors −
Background color changed = Blue Foreground color changed = Yellow
Using Multiple Colors in One Program
You can change colors multiple times within the same program to create colorful output −
using System;
class ColorDemo {
public static void Main(string[] args) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("This text is RED");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("This text is GREEN");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("This text is BLUE");
Console.ResetColor();
Console.WriteLine("This text uses default colors");
}
}
The output of the above code is −
This text is RED This text is GREEN This text is BLUE This text uses default colors
Resetting Colors
Use Console.ResetColor() to restore the default console colors. This is particularly useful when your program changes colors and you want to ensure the console returns to its original state −
using System;
class ResetDemo {
public static void Main(string[] args) {
Console.WriteLine("Default colors");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine("Changed to magenta text on white background");
Console.ResetColor();
Console.WriteLine("Back to default colors");
}
}
The output of the above code is −
Default colors Changed to magenta text on white background Back to default colors
Conclusion
The Console.ForegroundColor property allows you to customize text colors in console applications using the ConsoleColor enumeration. Always use Console.ResetColor() to restore default colors when your program completes to ensure a clean console state.
