There are already various preprocessor symbols defined that you can use. From the docs:
.NET Framework
Framework/Version |
Symbol |
.NET Framework (any version) |
NETFRAMEWORK |
.NET Framework 2.0 |
NET20 |
.NET Framework 3.5 |
NET35 |
.NET Framework 4.0 |
NET40 |
.NET Framework 4.5 |
NET45 |
.NET Framework 4.5.1 |
NET451 |
.NET Framework 4.5.2 |
NET452 |
.NET Framework 4.6 |
NET46 |
.NET Framework 4.6.1 |
NET461 |
.NET Framework 4.6.2 |
NET462 |
.NET Framework 4.7 |
NET47 |
.NET Framework 4.7.1 |
NET471 |
.NET Framework 4.7.2 |
NET472 |
.NET Framework 4.8 |
NET48 |
.NET Standard
Framework/Version |
Symbol |
.NET Standard (any version) |
NETSTANDARD |
.NET Standard 1.0 |
NETSTANDARD1_0 |
.NET Standard 1.1 |
NETSTANDARD1_1 |
.NET Standard 1.2 |
NETSTANDARD1_2 |
.NET Standard 1.3 |
NETSTANDARD1_3 |
.NET Standard 1.4 |
NETSTANDARD1_4 |
.NET Standard 1.5 |
NETSTANDARD1_5 |
.NET Standard 1.6 |
NETSTANDARD1_6 |
.NET Standard 2.0 |
NETSTANDARD2_0 |
.NET Standard 2.1 |
NETSTANDARD2_1 |
.NET Core
Framework/Version |
Symbol |
.NET [Core] (any version) |
NETCOREAPP |
.NET Core 1.0 |
NETCOREAPP1_0 |
.NET Core 1.1 |
NETCOREAPP1_1 |
.NET Core 2.0 |
NETCOREAPP2_0 |
.NET Core 2.1 |
NETCOREAPP2_1 |
.NET Core 2.2 |
NETCOREAPP2_2 |
.NET Core 3.0 |
NETCOREAPP3_0 |
.NET Core 3.1 |
NETCOREAPP3_1 |
.NET 5+
Framework/Version |
Symbol |
.NET 5.0 |
NET5_0 |
For example:
#if NETSTANDARD1_6
Console.WriteLine("This is .Net Standard 1.6");
#endif
#if NETCOREAPP2_0
Console.WriteLine("This is .Net Core 2.0");
#endif