Search code examples
c#visual-studiovisual-studio-2019compiler-directives

Switching between compiler directives in Visual Studio 2019?


I'm using Visual Studio 2019 with SDK based project, with multiple targets:

<TargetFrameworks>netstandard2.0;net45;net46</TargetFrameworks>

enter image description here

But when I write conditional code, I see (obviously) some code in gray and some code in regular colors :

enter image description here

Question:

What settings decide which section will be gray and which will not? Because now, if I want to edit the "NETFULL" section (because I'm multitargeting), it's all gray and I don't have intellisense.

How can I tell VS: now let's switch to NETFULL mode?

NB Sure I can remove the condition but I want to know why is it gray and how can I switch between them ( since I'm multitargeting)


Solution

  • To switch between the frameworks you are targeting use the dropdown at the top left of the code pane:

    enter image description here enter image description here


    Conditional-compilation symbols are declared in the .csproj file:

    <PropertyGroup Condition="'$(TargetFramework)' == 'net45' OR '$(TargetFramework)' == 'net46'">
      <DefineConstants>NETFULL</DefineConstants>
    </PropertyGroup>
    

    or you can use the predefined symbols such as NETFRAMEWORK, NETSTANDARD or NETCOREAPP (or the versioned predefined symbols such as NETSTANDARD2_0).