We are not talking about if statements here. We are talking
about the #if preprocessor token here. This token is processed
during compilation. If the symbol is defined at compilation time then the
encased code is compiled otherwise it is not. Compile-time symbols are defined
either through the command line or through the #define symbol.
#define DEBUG
#if DEBUG
public class MyDebug
{
}
#else
#endif
These tokens are similar (although far less useful) to the C++ versions. They
are not recommended for general use. Instead use the ConditionalAttribute.
The attribute can be applied to most types and members. The attribute does
basically the same thing as the preprocessor token. The difference lies in
the behavior of the compiler when the enclosed code is not included.
[Conditional("DEBUG")]
public class MyDebug
{
}
If the condition is not met then the code is completely removed from the assembly
during compilation. With the attribute the code is always compiled and included
in the assembly but is excluded at runtime if the condition was not met. For
debug, testing or tracing classes or members this is great as only one assembly
need to be generated containing all the types or members but they will only be used
if the condition is true when they are referenced later in the code.