Search code examples
c#.netc-preprocessorpreprocessor-directive

Programmatically check the build configuration


Using the DEBUG configuration, I can switch behaviour on and off using this type of syntax:

#if DEBUG
    Console.WriteLine("Debug");
#else
    Console.WriteLine("Not Debug");
#endif

However, if I set up a different configuration, say: TEST then this doesn't work:

#if TEST
    Console.WriteLine("Test");
#else
    Console.WriteLine("Not Test");
#endif

Is there a way to check these?


Solution

  • The DEBUG constant is a special one, and there's a setting for each project in each configuration whether it should be defined. The default is that it's on in Debug and off in Release, but it's completely configurable - open the properties page for a project and look under "Build", and there's a checkbox there saying "Define DEBUG constant."

    Thus, defining a new build configuration, does not automatically give you any other compile constants for free. But that doesn't mean you can't create them manually.

    To create a compile constant, add it to the list of "Conditional Compilation Symbols" - but make sure to do so in the correct build configuration.