4

In my projects Visual Studio gives me recommendations to use C# 8.0 features for Framework 4.6 projects, even though the default is specified on the Microsoft documentations to be 7.3. An example of one of the suggestions:

var i1 = "";
var i2 = "";
i1 = i1 ?? i2; //suggests i1 ??= i2;

example of suggestion

If I let VS correct the suggestions the IDE shows me no errors or warnings, but the moment I build I get a compiler error.

I know I can change the langversion in the .csproj file to get it to work, but that's not the point.

Why do I get suggestions and then proceed to get no errors until compilation?

In my memory I used to get suggestions to automatically upgrade the project to the langversion for 7.X features in the past, if I wanted to use features from that version.

Example of one of the .csproj files:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{00000000-0000-0000-0000-000000000000}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>MyProject</RootNamespace>
    <AssemblyName>MyProject</AssemblyName>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  //... References & compilation files
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
NotFound
  • 5,005
  • 2
  • 13
  • 33
  • 1
    show us the xml of your project file (*.csproj) – jazb Oct 04 '19 at 08:27
  • 2
    Weird, .NET 4.6 (4.7 and 4.8 as well) isn't supported by C# 8, as it needs .NET Standard 2.1 things to work. More [here](https://devblogs.microsoft.com/dotnet/building-c-8-0/) under "Platform Dependencies" – MindSwipe Oct 04 '19 at 08:30
  • 4
    My VS doesn't suggest anything like this, are you sure it's not an extension? Try starting CS in safe mode (`devenv.exe -SafeMode` from the command line) and see if it still happens. – DavidG Oct 04 '19 at 08:32
  • DavidG is right, might be ReSharper, it's been recommending C# 8 features to me for a while now – MindSwipe Oct 04 '19 at 08:35
  • @MindSwipe Not R# here either, unless it's an older version. – DavidG Oct 04 '19 at 08:35
  • @DavidG I have Visual Studio Enterprise 2019. No resharper, no other manually installed extensions. Just straight downloaded from the Visual Studio Installer. – NotFound Oct 04 '19 at 08:36
  • 1
    And VS is up to date? – DavidG Oct 04 '19 at 08:37
  • 1
    @DavidG Version 16.3.1, just saw there's a very new update out so I'm updating to 16.3.2. Also tried `-SafeMode`, but it yields the same results. – NotFound Oct 04 '19 at 08:39
  • @DavidG I have the same issue using 16.3.2 – NotFound Oct 04 '19 at 08:42
  • Works fine for me with VS 16.3.0. I do get that suggestion in .NET Core projects, but not in .NET Framework projects. – cremor Oct 04 '19 at 09:08
  • Have done a repair through the Installer, but yet again. Same results – NotFound Oct 04 '19 at 09:33
  • @404 looks like an Intellisense or analyzer bug. You can't use `??=` in C# 7.3, period. You'll *have* to change the language to C# 8 if you want to try this – Panagiotis Kanavos Oct 04 '19 at 09:37
  • 6
    https://developercommunity.visualstudio.com/content/problem/752720/ide0054-and-ide0063-triggered-when-not-using-c8.html – Hans Passant Oct 04 '19 at 09:53
  • 2
    According to [this more differentiated answer](https://stackoverflow.com/a/57020770/880990) it is possible to manually set the language version in the project file and to use some of the C#8.0 features in the old Framework. Purely syntactical stuff can be used. Default interface methods requiring changes in the runtime cannot be used. Indexes and ranges and also async streams which require new types in the framework cannot be used. – Olivier Jacot-Descombes Oct 16 '19 at 21:42
  • The [Developer Community post](https://developercommunity.visualstudio.com/content/problem/752720/ide0054-and-ide0063-triggered-when-not-using-c8.html) referenced by @HansPassant was converted to a [GitHub issue](https://github.com/dotnet/roslyn/issues/39430), but closed because, according to the comments, it only occurs when `latest` is set. But that doesn’t appear to be the case in the OP’s `csproj` sample, so it seems the issue may have gotten lost in translation to GitHub, likely based on one comment pushing it in that direction. Is this still occurring on your project? – Jeremy Caney Apr 04 '20 at 22:57

1 Answers1

0

Updating the Microsoft.Net.Compilers NuGet package worked for me.

I updated from 2.10.0 to 3.11.0.

Gerald
  • 1