2

Let's say you want to write a program in C# and compile it with command prompt. Assume the simple program only says;

 Console.WriteLine("a" + "b" + "c" + "d");

Is it possible to make a, b, c, d print out in different colors on command prompt?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
LTran
  • 131
  • 1
  • 7
  • 2
    http://stackoverflow.com/questions/7937256/changing-text-color-in-c-sharp-console-application But you'll need to Write each after changing the color and WriteLine on the "d". – kenny Feb 15 '13 at 22:06

3 Answers3

2

yes. you must change the color and print your text like:

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("a");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("b");
.
...
Hamed
  • 2,084
  • 6
  • 22
  • 42
1

Use Console.ForegroundColor property with ConsoleColor enumeration.

Gets or sets the foreground color of the console.

Like;

    public static void Main(string[] args)
    {
        Console.ForegroundColor = ConsoleColor.White;
        string a = "a";
        Console.WriteLine(a);
        Console.ForegroundColor = ConsoleColor.Blue;
        string b = "b";
        Console.WriteLine(b);
        Console.ForegroundColor = ConsoleColor.DarkGreen;
        string c = "c";
        Console.WriteLine(c);
        Console.ForegroundColor = ConsoleColor.Red;
        string d = "d";
        Console.WriteLine(d);
    }

Output will be;

enter image description here

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

The Console class has the ForegroundColor property that you can use:

Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(a);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(b);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(c);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(d);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005