Global conditional symbols in C#


Recently, I got slightly different questions from folks who weren't aware of the /define command-line compiler option in C#. The thinking was they needed to define a conditional-symbol (in specspeak) defined for their entire project and, to do so, they had to add the following to each of their source files to make sure it was defined everywhere:

#define myconditionalsymbol

The better (and much easier) way to do this is to define the conditional symbol to the compiler when you compile the code. An example of a command-line that does this follows:

csc /define:myconditionalsymbol source.cs

You can also achieve this via the Visual Studio IDE. In VS 2003 (aka v7.1 or Everett), you go to the project's properties -> Configuration Properties -> Build -> Conditional Compilation Constants. You'll find a semi-colon separated list there that you can add your own symbols to. In VS 2005 (aka 8.0 or Whidbey), it's in project properties -> Build -> Conditional Compilation Symbols.

Comments (3)

  1. Thanks for your answer.

    My real problem is that I’m making a C# parser and I have no problem to get information about symbol defined in the source code as

    #define <name_symbol>

    but to get information about symbols defined in conditional compilation constants in Visual Studio C# code.

    Thank you very much!

  2. Darren Oakey says:

    what I need, (and haven’t been able to find) is a way of specifying a global value to devenv! We have a set of guids which we want to be different in the production build. We use nant and devenv to build our system, and want to just say devenv /define production /build release master.sln

    We have 50-ish projects in the solution – so making and maintaining separate configurations called "productionRelease", and differing from "release" in only one respect – ie having PRODUCTION defined – is quite expensive 🙁

Skip to main content