views:

69

answers:

2

Hi I wonder how to compiler compile my code if i using #if directive inside my code. I would like to create special version of my application (commercial demo version) and I want to limit the functionality of my application. I would rather avoid the obfuscation and just don't want to add all my compiled code to executable file. I need solution resists preview my code during disasembly process. Can I use #if directives for variant compilation instead making comments for disabling code parts?

+6  A: 

Using the #if directive is like using the preprocessor in c++, in that the code would simply not be present if the condition hasn't been met (in compilation time). From MSDN:

When the C# compiler encounters an #if directive, followed eventually by an #endif directive, it will compile the code between the directives only if the specified symbol is defined. Unlike C and C++, you cannot assign a numeric value to a symbol; the #if statement in C# is Boolean and only tests whether the symbol has been defined or not.

ohadsc
I assume you meant "preprocessor" instead of "precompilation". #if in C++ preprocessor simply means the block of text is textually edited out before being passed to the real compiler if the condition is not met, i.e. the compiler does not even see the code inside #if, if the condition is not met.
Lie Ryan
You're right, thanks
ohadsc
+2  A: 

As an addition to @ohadsc's answer: you can always check with Reflector what is actually produced by the compiler.

Vlad