#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;
 }

http://wp.me/pXTis-hV directo

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

One Response to #113 – Conditionally Compiling Code Based on Symbols

  1. Pingback: #117 – Use #define to Define a Symbol « 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers