Search code examples
c#c-preprocessorconditional-compilation

Conditional compilation depending on the framework version in C#


Are there any preprocessor symbols which allow something like

#if CLR_AT_LEAST_3.5
// use ReaderWriterLockSlim
#else
// use ReaderWriterLock
#endif

or some other way to do this?


Solution

  • I don't think there are any predefined 'preprocessor' symbols. However you can achieve what you want like this:

    1. Create different configurations of your project, one for every version of CLR you want to support.

    2. Choose a symbol like VERSION2, VERSION3 etc. per CLR version.

    3. In every configuration, define the one symbol associated with it and undefine all others.

    4. Use these symbols in conditional compilation blocks.