Upgrade Project fixer should be able to upgrade TargetFramework to support new language versions
See original GitHub issueScenario: you have an existing netcoreapp3.1 project and you just installed VS 16.8. You want to try some of the new C# 9 features so you go into your code and add:
record Widget(string Name, int Id);
This gives some diagnostics about how there’s no type named ‘record’ in scope, this language feature requires LangVersion 9.0, etc. So you use the lightbulb menu to upgrade your project’s LangVersion to 9.0.
However, the TargetFramework in your project is unchanged. Instead of building successfuly, the fixed project has the diagnostic: CS0518 Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported
. This type is only present in net5.0, so you have to upgrade the TargetFramework or declare the type yourself (not officially supported) in order to use records. note: it’s a bug that the upgrade was offered to a language version not supported by the target framework. Tracked by #47983
Given this behavior, and given that we basically require using new TargetFramework in order to adopt new language versions (old TargetFrameworks are unsupported in combination with newer LangVersions), perhaps the UpgradeProject fixer should offer to change the TargetFramework instead of the LangVersion in certain scenarios.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:10 (10 by maintainers)
@RikkiGibson Thanks, I updated the title and removed the records label since it applies to all version-specific features starting with C# 8
This issue tracks the suggestion to automatically upgrade the user’s TargetFramework. #47983 tracks the bug where the UpgradeProject code fix is offered in scenarios it should not be offered in.