I want to highlight text in WriteLine how can I do it? For example Console.Write("Hello World"); and I want "World" to be in green color. Can you guys help me please?
Asked
Active
Viewed 248 times
-1
-
1Does this answer your question? [How do I change the full background color of the console window in C#?](https://stackoverflow.com/questions/7524057/how-do-i-change-the-full-background-color-of-the-console-window-in-c) – corashina Sep 30 '20 at 18:21
2 Answers
2
You can use Console.BackgroundColor property.
The best practice would be:
// save the previous color
var prevColor = Console.BackgroundColor;
// set your color
Console.BackgroundColor = ConsoleColor.Green;
Console.Write("Hello World");
Console.BackgroundColor = prevColor ;
// finally, reset all colors to originals.
Console.ResetColor();

EylM
- 5,967
- 2
- 16
- 28
1
Example ( my default text color is Green )
static void Main(string[] args)
{
Console.Write("Hello");
var prevColor = Console.ForegroundColor;
// Set new text's color
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(" World");
// Restore old text's color
Console.ForegroundColor = prevColor;
Console.ReadLine();
}

VietDD
- 1,048
- 2
- 12
- 12