4

Is there any predefined constant to distinguish language versions, such as:

#if C#6
 //code
#else
 //code
#endif
Viliam
  • 636
  • 9
  • 17
  • Are you trying to work with two different versions of languages within the same project? – Dennis Lukas Apr 10 '19 at 14:39
  • 1
    Take a look at: [#if (C# Reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if). Not an answer for the LANGUAGE version, but for the Framework version – JayV Apr 10 '19 at 14:42
  • 1
    What exactly are you trying to achieve? Why not just write code which works on your lowest supported language version, rather than both code which works on the lowest supported *and* code which works on C# 6? – canton7 Apr 10 '19 at 14:43
  • Thank you all for your comments and recommendations. The reason for my question was that my code written in the new language version, I needed to use also for a project in older Visual Studio with an old compiler. I did not want to just rewrite several places in the code and have two versions. I would prefer to have one almost universal code. And I was also sorry to throw away the modern part of the code and keep the lowest one. – Viliam Apr 10 '19 at 19:00

2 Answers2

1

You shouldn't have to do this.

Typically, you would do something like this to support different versions of .NET framework. Not different versions of the C# compiler. Newer compiler can target older versions of the framework. In short, use the latest C# compiler features. The compiler is free, so there isn't any real roadblock to updating a project to use the latest features.

MarkPflug
  • 28,292
  • 8
  • 46
  • 54
0

Depending on your environment you can rely on the platform version or not. If the projects do not use custom explicit language versions, you can try to use the target framework directives.

The compiler determines a default based on these rules: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#defaults

.NET Framework
NETFRAMEWORK, NET48, NET472, NET471, NET47, NET462, NET461, NET46, NET452, NET451, NET45, NET40, NET35, NET20

.NET Standard
NETSTANDARD, NETSTANDARD2_1, NETSTANDARD2_0, NETSTANDARD1_6, NETSTANDARD1_5, NETSTANDARD1_4, NETSTANDARD1_3, NETSTANDARD1_2, NETSTANDARD1_1, NETSTANDARD1_0

.NET 5+ and .NET Core
NET, NET6_0, NET6_0_ANDROID, NET6_0_IOS, NET6_0_MACOS, NET6_0_MACCATALYST, NET6_0_TVOS, NET6_0_WINDOWS, NET5_0, NETCOREAPP, NETCOREAPP3_1, NETCOREAPP3_0, NETCOREAPP2_2, NETCOREAPP2_1, NETCOREAPP2_0, NETCOREAPP1_1, NETCOREAPP1_0

https://stackoverflow.com/questions/38476796/how-to-set-net-core-in-if-statement-for-compilation

So you can use if-else directives with these keywords

#if NET5_0
// GG C# 9
#endif

There are also other directive keywords with the higher or lower keyword

#if NET5_0_OR_GREATER  // up-level version
#else  // down-level version
#endif

This approach is unreliable if you actually need only language version specific features. I recommend to set the latest language version in your csproj.

Vadim
  • 96
  • 1
  • 8