Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

As the title states, I was wondering what are the differences between using

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

and

if (debug)
   DoWork();
else
   DoAnotherWork();
like image 711
Roman Ratskey Avatar asked Feb 23 '13 00:02

Roman Ratskey


2 Answers

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

like image 53
Daniel Wardin Avatar answered Oct 02 '22 03:10

Daniel Wardin


First is Preprocessor Directive and second Logical statement.

like image 30
Vano Maisuradze Avatar answered Oct 02 '22 03:10

Vano Maisuradze