Runtime: Overload Console.Write and Console.WriteLine to take ConsoleColor as a parameter

Created on 22 Jul 2020  路  20Comments  路  Source: dotnet/runtime

(I was directed to repost this feature request here.)

Background and Motivation

I'm trying to create a user-friendly command-line application, and as such I wanted to color my output to the console. However, I found myself frequently writing code like this:

Console.WriteLine("This output is white on black.");
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Green;
Console.WriteLine("This output is red on green.");
Console.ResetColor();
Console.WriteLine("This output is white on black.");

Proposed API

I thought it might be a lot easier for Console to have an overload on Write and WriteLine that takes in two ConsoleColor elements as arguments for the ForegroundColor and BackgroundColor variables and abstracts this process away from the developer. This way, the process of setting the ForegroundColor and BackgroundColor variables, writing to the console, and resetting the color can be written as one line in code. I think something like the below would work:

+ public static void WriteLine(ConsoleColor foreground, ConsoleColor background, string value) { throw null; }

Usage Examples

Console.WriteLine("This output is white on black.");
Console.WriteLine(ConsoleColor.Red, ConsoleColor.Green, "This output is red on green.");
Console.WriteLine("This output is white on black.");

would be equivalent to the code in the Background and Motivation section.

Alternative Designs

If overloading is not possible, additional methods like WriteColor and WriteLineColor would still be very useful in my opinion.

Risks

I'm not really sure if there are any risks that come with making this change. I don't think there would be because the functionality would just use the existing methods in Console and the existing ConsoleColor enum.

api-suggestion area-System.Console

Most helpful comment

All 20 comments

Was this page helpful?
0 / 5 - 0 ratings

Related issues

btecu picture btecu  路  3Comments

EgorBo picture EgorBo  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments

omajid picture omajid  路  3Comments

sahithreddyk picture sahithreddyk  路  3Comments