C# code in templates not supported by earlier versions

Starting in .NET 6, the project templates that ship with the .NET SDK use the latest C# language features. The following language features are used in template-generated code or enabled by default in the project:

Some of the latest C# language features are not supported by previous target frameworks, so you might experience issues in the following scenarios:

  • When you use a .NET 6 template and then change the target framework to a previous version.
  • When you use a .NET 6 template and then multi-target by adding a previous target framework version.

Version introduced

.NET SDK 6.0.100

Old behavior

In previous .NET versions, you can change the target framework to a previous version or add an additional version without having to change the C# code created by the project template.

New behavior

Starting in .NET 6, when you change or add a target framework, you may need to change the C# code generated by the template to avoid using unsupported language features. The project won't build without these changes. The compiler errors and warnings usually guide you on how to change the generated code to make it compatible with new target framework.

Change category

This change affects source compatibility.

Reason for change

We want the project templates to be synchronized with the latest language features. The main usage scenario of targeting the latest framework uses the latest C# language features.

If you encounter compile-time errors and warnings when you retarget to an earlier framework version, use that information to guide you in changing the generated code to make it compatible with the target framework you selected.

To avoid compile-time errors when targeting an older framework, follow these suggestions:

  • Avoid changing the target framework to a previous version. Instead, select the target framework you want during project creation in Visual Studio or at the command line by using dotnet new <templateName> --framework <targetFramework>.
  • When creating a multi-target project, select the lowest target-framework version when you create the project. Add the additional target frameworks after the project is created.

Affected APIs

N/A