4

I'm just beginning to learn C# and am curious if there is an ASCII way to get colored output in the terminal, for example:

In Ruby I can do this:

puts "\e[32mThis will be green\e[0m"

In JavaScript I can do this:

function say(input){ 
    console.log("\033[32m" + input + "\033[0m"
}

say("This will be green")

How can I do the same thing in C#?

13aal
  • 1,634
  • 1
  • 21
  • 47

2 Answers2

3

C# you an use the following:

Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("White on blue.");
Console.Read();

Remember you can also do this in JavaScript:

console.log('%cHello world', 'background-color: #0000FF; color: #FFFFFF');

Run the code snippet and then look at the JavaScript console.

gotnull
  • 26,454
  • 22
  • 137
  • 203
2

In a Console App you can do

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Hi I'm green");
Console.Read();
etoisarobot
  • 7,684
  • 15
  • 54
  • 83