#113 – Conditionally Compiling Code Based on Symbols

In addition to conditionally compiling code based on the DEBUG symbol, you can define your own new symbols and then use the #if directive to indicate that a block of code should only be compiled when that symbol is defined.

For example, let’s assume that you want to easily be able to include/exclude code that does some logging, but you don’t want to necessarily do this in only the Debug configuration.

Select the build configuration in which you want the symbol defined:

Right-click on the project and select Properties.  In the project properties window, click on the Build tab.

Now you can add your own symbol in the “Conditional compilation symbols” textbox:

Finally, use the #if directive to identify code that should only be built when the LOGGING symbol is present.

 static void Main(string[] args)
 {
#if LOGGING
     DoSomeLogging();   // Only do this in Debug build
#endif
    uint x = 0x1234;
    x &= 0x0020;
 }
Advertisements