Defining your own symbols is probably cleaner, but I was curious as to what the compiler would do, so I did some experimenting (using VS 2008 in release mode).
With this:
class Program
{
static void Main(string[] args)
{
bool foo = false;
if (foo)
{
Console.WriteLine("Hello, world?");
}
}
}
The compiler still generates the code for the if statement:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 16 (0x10)
.maxstack 1
.locals init ([0] bool foo)
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: brfalse.s IL_000f
IL_0005: ldstr "Hello, world\?"
IL_000a: call void [mscorlib]System.Console::WriteLine(string)
IL_000f: ret
} // end of method Program::Main
If instead you do:
class Program
{
static void Main(string[] args)
{
bool foo = false;
if (false)
{
Console.WriteLine("Hello, world?");
}
}
}
It doesn't generate code for the if statement:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 1 (0x1)
.maxstack 8
IL_0000: ret
} // end of method Program::Main
It also seems to skip the variable that is assigned a value that is never used.
I checked with ildasm.exe, the disassembler that comes with visual studio.