Defference Between #if #else #endif And Regular if, else

.net, c#

Solution

if (debug)
    DoWork();
else
    DoAnotherWork();

The above code will be compiled and the condition will be checked at runtime.

#if
    DoWork();
#else
    DoAnotherWork();
#endif

These statements will be checked at compile time.

So if #if condition is true, DoWork(); will be compiled and otherwise DoAnotherWork(); will be compiled. Where as in the previous example all code including the if statement will be compiled.

Please read this on Preprocessor Directives

Preprocessor Directives

Problem

As the title states, I was wondering what are the differences between using ``` #if DoWork(); #else DoAnotherWork(); #endif ``` and ``` if (debug) DoWork(); else DoAnotherWork(); ```

Original source