I want to write one line in another background and foreground color. The straightforward solution changes the background for the space at the end of the line that follows, too, which looks bad.
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("----------------------CoNet------------------------");
Console.ResetColor();
Console.WriteLine("Akkoord? type: ja of nee: "); // The rest of this line gets blue. Why?
The first line is completely white on blue, which is OK. The second line should be completely gray on black (default), but the free space right to its contents is all blue.
How is this possible and what did I do wrong?
This happened because your Console.WriteLine() call forced the console window to scroll. Your snippet cannot reproduce it because it writes to the top of the console buffer, not the bottom like your real program does. Or in other words, your real program already generated a bunch of output. Get a repro by inserting this line at the top of your Main() method:
for (int ix = 0; ix < Console.BufferHeight; ++ix) Console.WriteLine();
And yes, scrolling forces another line to get added to the output buffer, it is inevitably colored with the default colors that are in effect at the time of the WriteLine() call. Your next Console.WriteLine() call does not completely reset it.
There is no super-smooth way to avoid it. You'll probably also won't like:
Console.Write("------------------------------test------------------------------");
Console.ResetColor();
Console.WriteLine();
Console.WriteLine("test123 blablablablablabla: ");
Albeit that it is now consistent.