How to color words in different colours in a Console.WriteLine in a console application?

The last sentence of my code is a Console.WriteLine with a variable. I would like to have the text between "" to be green and the variable to be red.

I have been trying with Console.Foregroundcolor, but this wasn't successful.

Console.WriteLine("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("Your name is {0}.", name);
Console.ReadKey();

An slight improvement on currarpickt's answer:

public void Write(params object[] oo)
{
  foreach(var o in oo)
    if(o == null)
      Console.ResetColor();
    else if(o is ConsoleColor)
      Console.ForegroundColor = o as ConsoleColor;
    else
      Console.Write(o.ToString());
}

Now you can mix any number of text and color:

 Write("how about ", ConsoleColor.Red, "red", null, " text or how about ", ConsoleColor.Green, "green", null, " text");

Using null puts the color back to default

Or how about we build a parser:

public void Write(string msg)
{
  string[] ss = msg.Split('{','}');
  ConsoleColor c;
  foreach(var s in ss)
    if(s.StartsWith("/"))
      Console.ResetColor();
    else if(s.StartsWith("=") && Enum.TryParse(s.Substring(1), out c))
      Console.ForegroundColor = c;
    else
      Console.Write(s);
}

And we can use like:

Write("how about {=Red}this in red{/} or this in {=Green}green{/} eh?");

Should tidy things up. It's a really simple unsophisticated parser though, you'll need to improve it if your strings contain { or } for example


You can't use different colors within one Console.WriteLine() - use Console.Write() instead.

Console.WriteLine("What is your name?");
string name = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("Your name is ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("name");
Console.WriteLine(); //linebreak
Console.ResetColor(); //reset to default values

I liked Caius Jard's parser answer, but I improved upon it a little bit so you can change both the background color and the foreground color, and even nest colors. I've created a new static class called ConsoleWriter.

To set foreground color:

ConsoleWriter.WriteLine("{FC=Red}This text will be red.{/FC}");

To set background color:

ConsoleWriter.WriteLine("{BC=Blue}This background will be blue.{/BC}");

It even keeps track of the history of colors you used in a single call, that way you can actually nest colors like this:

ConsoleWriter.WriteLine("{FC=Magenta}This is magenta, {FC=Yellow}now yellow, {/FC}now back to magenta{/FC}, now back to default.");

This will output as: Nested colors output

I even like to use the actual enum in an interpolated string and it still works:

ConsoleWriter.WriteLine($"{{FC={ConsoleColor.Red}}}This works too!{{/FC}}");
public static void Write(string msg)
{
  if (string.IsNullOrEmpty(msg))
  {
    return;
  }

  var color_match = Regex.Match(msg, @"{[FB]C=[a-z]+}|{\/[FB]C}", RegexOptions.IgnoreCase);
  if (color_match.Success)
  {
    var initial_background_color = Console.BackgroundColor;
    var initial_foreground_color = Console.ForegroundColor;
    var background_color_history = new List<ConsoleColor>();
    var foreground_color_history = new List<ConsoleColor>();

    var current_index = 0;

    while (color_match.Success)
    {
      if ((color_match.Index - current_index) > 0)
      {
        Console.Write(msg.Substring(current_index, color_match.Index - current_index));
      }

      if (color_match.Value.StartsWith("{BC=", StringComparison.OrdinalIgnoreCase)) // set background color
      {
        var background_color_name = GetColorNameFromMatch(color_match);
        Console.BackgroundColor = GetParsedColorAndAddToHistory(background_color_name, background_color_history, initial_background_color);
      }
      else if (color_match.Value.Equals("{/BC}", StringComparison.OrdinalIgnoreCase)) // revert background color
      {
        Console.BackgroundColor = GetLastColorAndRemoveFromHistory(background_color_history, initial_background_color);
      }
      else if (color_match.Value.StartsWith("{FC=", StringComparison.OrdinalIgnoreCase)) // set foreground color
      {
        var foreground_color_name = GetColorNameFromMatch(color_match);
        Console.ForegroundColor = GetParsedColorAndAddToHistory(foreground_color_name, foreground_color_history, initial_foreground_color);
      }
      else if (color_match.Value.Equals("{/FC}", StringComparison.OrdinalIgnoreCase)) // revert foreground color
      {
        Console.ForegroundColor = GetLastColorAndRemoveFromHistory(foreground_color_history, initial_foreground_color);
      }

      current_index = color_match.Index + color_match.Length;
      color_match = color_match.NextMatch();
    }

    Console.Write(msg.Substring(current_index));

    Console.BackgroundColor = initial_background_color;
    Console.ForegroundColor = initial_foreground_color;
  }
  else
  {
    Console.Write(msg);
  }
}

public static void WriteLine(string msg)
{
  Write(msg);
  Console.WriteLine();
}

private static string GetColorNameFromMatch(Match match)
{
  return match.Value.Substring(4, match.Value.IndexOf("}") - 4);
}

private static ConsoleColor GetParsedColorAndAddToHistory(string colorName, List<ConsoleColor> colorHistory, ConsoleColor defaultColor)
{
  var new_color = Enum.TryParse<ConsoleColor>(colorName, true, out var parsed_color) ? parsed_color : defaultColor;
  colorHistory.Add(new_color);

  return new_color;
}

private static ConsoleColor GetLastColorAndRemoveFromHistory(List<ConsoleColor> colorHistory, ConsoleColor defaultColor)
{
  if (colorHistory.Any())
  {
    colorHistory.RemoveAt(colorHistory.Count - 1);
  }

  return colorHistory.Any() ? colorHistory.Last() : defaultColor;
}

If you wanna make different color to each text on console you should write Console.BackgrundColor and Console.ForeGroundColor before each input and output in consolse. For example:

        Console.BackgroundColor = ConsoleColor.Yellow;
        Console.ForegroundColor = ConsoleColor.Red;

        Console.WriteLine("Enter your name:");

        string name = Console.ReadLine();

        Console.BackgroundColor = ConsoleColor.Green;
        Console.ForegroundColor = ConsoleColor.Yellow;

        Console.WriteLine("Hello, " + name);

        Console.ReadKey();

You could make a method for that:

public void ColoredConsoleWrite(ConsoleColor firstColor, string firstText, ConsoleColor secondColor, string secondText)
{
    Console.ForegroundColor = firstColor;
    Console.Write(firstText);
    Console.ForegroundColor = secondColor;
    Console.WriteLine(secondText);
}

And call it later like this:

ColoredConsoleWrite(ConsoleColor.Green, "Your name is ", ConsoleColor.Red, name);