February 6, 2026

C# Dot Net SDK Version Macros

It is useful to be able to include or exclude code based on preprocessor macros. We can do this for the different versions of .NET

1. Use "#if" with framework version symbols to include specific code for specific .Net versions .NET SDK defines symbols like "Net5_0", "Net6_0", "Net7_0", etc. For .NET 10, the symbol will be "Net10_0". This works automatically if you're targeting .NET 10 via the SDK-style project and using the element <TargetFramework> like: "<TargetFramework>net10.0<TargetFramework>". Here is an example of this:

#if NET10_0
    // This code will be included when targeting .NET 10
    Console.WriteLine("Running on a framework other than .NET 10");
#endif
🧠 These symbols can be combined for example:

#if NET6_0 || NET7_0
    Console.WriteLine("Running on .NET 6 or 7");
#elif NET10_0
    Console.WriteLine("Running on .NET 10");
#endif
2. Invert the macro condition to exclude code using the ! sign For example, you can use "#if !NET10_0" to exclude code when the target is not .NET 10. Here is an example of this:

#if !NET10_0
    // This code will be excluded when targeting .NET 10
    Console.WriteLine("Running on a framework other than .NET 10");
#endif
3. You can also use a "_OR_GREATER" suffix, for example:

#if NET5_0_OR_GREATER
    // This code will be included when targeting .NET 5 or greater
#endif
There are also platform-specific preprocessor symbols for conditional compilation based on the operating system. These are especially useful when writing cross-platform code in .NET Core or .NET 5+. 🧭 Built-in platform symbols Here are the most common ones:
OS Target
WINDOWS Compiling for Windows
LINUX Compiling for Linux
OSX Compiling for Mac OS
ANDROID Compiling for Android
IOS Compiling for iOS
MACCATALYST Compiling for Mac Catalyst
FREEBSD Compiling for FreeBSD
BROWSER Compiling for WebAssembly (Blazor)
These are automatically defined by the SDK when targeting specific platforms.
✅ Example usage

#if WINDOWS
    Console.WriteLine("Running on Windows");
#elif LINUX
    Console.WriteLine("Running on Linux");
#elif OSX
    Console.WriteLine("Running on macOS");
#else
    Console.WriteLine("Unknown platform");
#endif

🔍 Notes

  • These symbols are only defined when the runtime identifier (RID) or target platform is specified appropriately in your ".csproj " file.
  • If you're multi-targeting or using runtime checks, you might prefer OperatingSystem.IsWindows() or similar APIs from System.Runtime.InteropServices

Note that this is heavily edited output from CoPilot.