Skip to content

Don't define NETCOREAPP3_1 when targeting .NET 5 #20692

@terrajobst

Description

@terrajobst

Don't define NETCOREAPP3_1 when targeting .NET 5

.NET 5 will now have the same behavior for defining preprocessor symbols as previous releases of .NET Core, that is, we only define symbols for what the project is actually targeting and no longer include defines for earlier versions.

Version introduced

.NET 5 RC2

Old behavior

Since .NET 5 Preview 7, targeting net5.0 defined both NETCOREAPP3_1 and NET5_0 preprocessor symbols. The intent was that starting with .NET 5, conditional compilation symbols would be accumulative.

New behavior

We'll revert this change which will make the behavior consistent with how preprocessor symbols were defined in previous releases of .NET Core (1.0 - 3.1).

Reason for change

Customer feedback. The new behavior was surprising and caused confusion on how to accommodate it. People assumed this was a bug in the C# compiler.

Recommended action

Ensure your #if logic doesn't assume that NETCOREAPP3_1 is defined when the project is targeting net5.0 or higher. Instead, assume that NETCOREAPP3_1 is only defined when the project targets netcoreapp3.1 exactly.

For example, if your project multitargets for .NET Core 2.1 and .NET Core 3.1 and you need to call APIs that were introduced in .NET Core 3.1, your #if logic should look as follows:

#if NETCOREAPP2_1 || NETCOREAPP3_0
    // Fallback behavior for old versions
#elif NETCOREAPP
    // Behavior for .NET Core 3.1 or later
#endif

Issue metadata

  • Issue type: breaking-change

Metadata

Metadata

Assignees

Labels

🏁 Release: .NET 5Work items for the .NET 5 releasebreaking-changeIndicates a .NET Core breaking change

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions