console.foregroundcolor c#

console.foregroundcolor c#
In this article [Show more]

    Console.ForegroundColor in C#

    In C#, the Console class allows developers to customize the appearance of text output through the console, including the ability to change the text color using the ForegroundColor property. This article explains how to use Console.ForegroundColor to modify text colors, providing practical examples and best practices.

    Understanding Console.ForegroundColor

    • Property Type: ConsoleColor enumeration.
    • Default Value: Inherits the system's console text color.
    • Available Colors: The ConsoleColor enumeration provides 16 color values such as Red, Blue, Green, Yellow, etc.

    Example 1: Changing Text Color

    Here's a basic example demonstrating how to change the text color in the console output:

     

    using System;
    
    public class ForegroundColorExample
    {
        public static void Main()
        {
            // Set the foreground color to Cyan
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("This text is in Cyan!");
    
            // Set the foreground color to Yellow
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("This text is in Yellow!");
    
            // Set the foreground color to Red
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("This text is in Red!");
    
            // Reset to the default console colors
            Console.ResetColor();
            Console.WriteLine("Default console colors restored.");
        }
    }
    

    Example 2: Conditional Color Coding

    You can also use different colors based on conditions to emphasize different types of messages:

     

    using System;
    
    public class ConditionalColorExample
    {
        public static void Main()
        {
            // Information message in White
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Information: The operation was successful.");
    
            // Warning message in Yellow
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Warning: The disk space is low.");
    
            // Error message in Red
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Error: An unexpected error occurred!");
    
            // Reset to the default console colors
            Console.ResetColor();
        }
    }
    

    Practical Applications

    • Logging: Differentiate log levels (information, warning, error) with specific colors.
    • User Prompts: Make prompts stand out for better clarity.
    • Aesthetics: Improve readability and the visual appeal of your console applications.

    Best Practices

    • Contrast: Ensure that the text color contrasts well with the background for better readability.
    • Reset Colors: Always reset to the default console colors to avoid affecting subsequent output.

    Conclusion

    The Console.ForegroundColor property in C# provides a simple yet effective way to customize text output in the console. By leveraging different colors, developers can enhance the usability and visual appeal of their console applications.

    Author Information
    • Author: Ehsan Babaei

    Send Comment



    Comments