I am following a tutorial that has this code:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
int i = 1;
while (true)
{
#if TEST
if (i % 2 == 0)
continue;
#endif
i++;
Console.WriteLine("The value of i is {0}", i);
if (i > 9)
break;
}
Console.WriteLine("The value of i is {0}", i);
Application.Run(new Form1());
}
What is the purpose of using #if TEST instead of just if(TEST)?
It’s useful when you need two (or more) versions of your code with little difference. Then instead of keeping two projects using complier directives like
#if TESTyou write both versions in the same project. Then from project properties you can set value for TEST llike TEST = true and then compile the project.If TEST = true it’s like you just write :
Console.WriteLine("Hello World!");and vice versa.