Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C# compile code inside an if(false) block?

Tags:

c#

.net

I am just wondering if these code blocks gets compiled into .dll

I don't think this one gets compiled at all

#if SOMETHING_UNDEFINED
// some code - this is ignored by the compiler
#endif

Now what about these?

1.

if(false) {
  // some code - is this compiled?
}

2.

const bool F = false;
if(F) {
  // some code - is this compiled?
}

3.

bool F = false;
if(F) {
  // some code - is this compiled?
}

EDIT: Sorry, I was talking about Visual Studio

like image 261
Aximili Avatar asked May 06 '10 07:05

Aximili


1 Answers

Just testing it, the Microsoft C# 4 compiler doesn't, and it looks like the Mono gmcs compiler version 2.4.0.0 doesn't either. I don't know that there's anything in the spec prohibiting it though.

EDIT: When I answered this, only the first version was present. Case 2 is equivalent to case 1, but case 3 isn't.

like image 68
Jon Skeet Avatar answered Sep 21 '22 15:09

Jon Skeet