Conditional compilation symbols in Microsoft Visual Studio

If you need to make breaking changes in your source code that will be deployed later to production, but first have to be tested in the development environment you can include conditional compilation symbols to specify which lines will be included in the application for certain environments. This way you can maintain environment specific versions of the source code, the current version for  production and the new version for development and testing.

In this example we will make a change in our source code that we want to deploy to the Development environment for testing, but not to QA, UAT and Production.

Create the conditional compilation symbol in the project

  • Right click the project
  • Select Properties
  • On the left side select the Build tab
  • Select Dev in the Configuration dropdown
  • Enter new_version into the Conditional compilation symbols field

visual studio conditional compilation symbol in project

 

Add the conditional compilation symbol to the source code

  • Surround the old and new code with a #if, #else, #endif structure

#if new_version

// New code
int a = 1;

#else

// Old code
int a = 0;

#endif

int b = a;

If the Dev configuration is selected the new code will be compiled into the application

visual studio conditional compilation symbol true

 

If another configuration (QA) is selected the old code will be compiled into the application

visual studio conditional compilation symbol false

One thought on “Conditional compilation symbols in Microsoft Visual Studio

Leave a Reply