I'm Junior programmer I Wrote this Code in Console application

static void Main(string[] args)
{
    Console.BackgroundColor = ConsoleColor.Red;
    Console.WriteLine("backgroundcolor is red");
    Console.ForegroundColor = ConsoleColor.Green;
    Console.Write("ForegroundColor is Green");
    Console.ReadKey();
}

But I want to Write one console write line. So background color is red just with background color in console and Foreground Color is Green write in console application just with Foreground Color at a same time that each sentence effected with its class in one line.

有帮助吗?

解决方案

What about this...

static void Main(string[] args)
{
 var originalColor = Console.BackgroundColor;
 Console.BackgroundColor = ConsoleColor.Red;
 Console.Write("The background color is red. ");
 Console.BackgroundColor = originalColor;
 Console.ForegroundColor = ConsoleColor.Green;
 Console.Write("The foreground color is green");
 Console.ReadKey();
}

其他提示

I think is what you want. Correct me if I am wrong.

static void Main(string[] args)
    {
        var originalColor = Console.BackgroundColor;
        Console.BackgroundColor = ConsoleColor.Red;
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("The BackgroundColor is " + Console.BackgroundColor.ToString() + " and the ForegroundColor is " + Console.ForegroundColor.ToString());
        Console.ReadKey();
    }

I think this will fix your problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top