2

My project in Debug mode contains constructs that only supports C# 7.3. But in Release mode, the project should be built on C# 7.0 without specific code lines.

I know about some standard preprocessor symbols like NET472 and NETSTANDARD2_0 but it uses to work with different standards and their versions. And it useless for language version condition.

public static Expression<TDelegate> CreateExpression<TDelegate>()
    where TDelegate : Delegate // Work in 7.3 and above
{ ... }

I expected some tricks like

#if CSharpVersion >= 7.3
   where TDelegate : Delegate
#endif

Now I use that statement:

#if DEBUG
    where TDelegate : Delegate
#endif

But it will not work if I change language versions in project properties.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
groser
  • 319
  • 1
  • 11
  • 2
    What is the usecase for having this different compilation process? Why not compile release in 7.3 as well? – Lasse V. Karlsen May 08 '19 at 10:09
  • And to answer your question, there's no built in variables for the compiler version. You will have to manage this on your own. – Lasse V. Karlsen May 08 '19 at 10:09
  • 1
    C# does not have a preprocessor, the #if directive was intentionally crippled to not allow expressions. Intentionally, they did not want to recreate the C and C++ nightmare. The pragmatic thing to do is not change the language version. Or to define "CSharpVersion73" in the project properties so you can make #if work. – Hans Passant May 08 '19 at 11:09

1 Answers1

0

If you are using the DEBUG define to choose the language version, you can do it in your .csproj file:

<Project ..>

  <PropertyGroup>
    ...
    <LangVersion>7.0</LangVersion>
    <LangVersion Condition="'$(Configuration)' == 'Debug'">7.3</LangVersion>
    ...
  </PropertyGroup>

  ...

</Project>

Beware that editing the project settings in the UI might changed this.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
  • What do you mean? VS has already automatically mapped the language version and configuration in .csproj file. I use `#if DEBUG` to remove a few lines of code in the RELEASE because C#7.3 is only used in debug at the moment. But this trick won't work if I change the configuration settings. – groser May 08 '19 at 17:04
  • And, with this, in debug configuration the language version will be 7.3. Otherwise, it will be 7.0. Wasn't that what you wanted? – Paulo Morgado May 08 '19 at 17:52
  • thanks. But I was expecting some **preprocessor tricks** that would allow me to work with **any combination** of language version and configuration. For example, using `$"x={x}"` and `String.Format("x={0}", x)` when compiling in versions 6.0(+) and 5.0(-) respectively. – groser May 08 '19 at 18:41
  • You can only use one compiler per project. – Paulo Morgado May 08 '19 at 22:08
  • I have a suggestion: Why not add `CS_7_3;...` instead? This way, you define your constant in your project when the language version is of a given number. I don't know if this works, I have not tried it myself, but may try in the near future. – Adam L. S. Dec 03 '21 at 09:11