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
Console.ResetColor() Method in C#
The Console.ResetColor() method in C# is used to reset the foreground and background console colors to their default values. This method is particularly useful when you have changed the console colors during program execution and want to restore the original appearance.
Syntax
Following is the syntax for the Console.ResetColor() method −
public static void ResetColor();
Parameters
This method takes no parameters.
Return Value
This method returns void (nothing).
Using Console.ResetColor() Method
The method restores both foreground and background colors to the system defaults. Here's how colors work in console applications −
Example
using System;
class Demo {
public static void Main(string[] args) {
Console.WriteLine("At first, we have set colors here...\nAfter that, we will reset the colors to default.");
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nColors are set for this text");
Console.ResetColor();
Console.WriteLine("\nNow we reset the color to default!");
}
}
The output shows the color changes visually in the console window. The middle line appears with white background and red text, while the first and last lines use default colors.
Using ResetColor() with Multiple Color Changes
Example
using System;
class ColorDemo {
public static void Main() {
Console.WriteLine("Normal text in default colors");
// Set first color scheme
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("Yellow text on blue background");
// Set second color scheme
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine("Green text on black background");
// Reset to defaults
Console.ResetColor();
Console.WriteLine("Back to default colors");
// Verify reset worked
Console.WriteLine("This line also uses default colors");
}
}
This example demonstrates how ResetColor() restores the original console appearance after multiple color changes.
Common Use Cases
-
Error Messages: Set red colors for errors, then reset to normal.
-
Status Indicators: Use colors to highlight success/failure, then reset.
-
Menu Systems: Color menu options, reset for user input areas.
-
Data Highlighting: Emphasize important data with colors, reset for regular text.
Conclusion
The Console.ResetColor() method is essential for maintaining clean console output when using colored text. It takes no parameters and simply restores both foreground and background colors to their system defaults, ensuring your console applications return to a neutral appearance after displaying colored content.
