Search code examples
c#intellisensepreprocessorrider

How do I create a preprocessor "define" in Jetbrains Rider


  • I am writing C# code using Jetbrains Rider.
  • I would like to compile different behaviours depending on certain circumstances (as specified by my preprocessor constant). The idea is to have code such as this
#if MYCONSTANT
CallAGivenFunction();
#else
CallAnotherFunction();
#endif

So, if I want the first branch to be compiled (and seen by the Intellisense), I will define a preprocessor constant called MYCONSTANT; otherwise (by doing nothing), I will have the second branch be compiled.

Looking through the documentation I understood that I should create a file named Directory.build.props in the same folder as my .csproj, and that file should have

<Project>
    <PropertyGroup>
        <DefineConstants>MYCONSTANT</DefineConstants>
    </PropertyGroup>
</Project>

However after doing that, the code in the #if clause CallAGivenFunction(); is still greyed out (i.e. Rider does not recognize the preprocessor constant I have defined). What am I doing wrong?


Solution

  • Within your .csproj file, some preprocessor directives are defined using the same <DefineConstants></DefineConstants> element that you are using within Directory.build.props. The element within .csproj overrides anything you create within Directory.build.props For your own constants to be be taken into account, you need to precede the element in .csproj with $(DefineConstants) (for example)

    <DefineConstants>$(DefineConstants);DEBUG;TRACE;</DefineConstants>