Usually when I make console applications I start with something like this:
using System;
namespace ColorOptProblemTest
{
class Program
{
const string HELP_TEXT = "";
static ConsoleColor Default = Console.ForegroundColor;
static void ShowHelp()
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(HELP_TEXT);
Console.ForegroundColor = Default;
Environment.Exit(0);
}
static void ThrowError(string Message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("ERROR: " + Message);
ShowHelp();
}
static void ManageArgs(string[] args)
{
switch (args.Length)
{
case 0:
ThrowError("At least one argument is required.");
break;
}
}
static void Main(string[] args)
{
ManageArgs(args);
}
}
}
The important part is that I save Console.ForegroundColor
for later when the program exits, so that the terminal can return to its original color.
Without compiler optimization, the code does everything like it's supposed to and works like a charm. With optimization, when the program exits, the color of the terminal is red.
Could it be, that this is some sort of bug in the optimizer? Maybe I'm just missing something elementary.