2

We have an issue with Visual Studio 2019 found when trying to pre-compile our ASP.Net project during publish. We got errors such as:

error CS1056: Unexpected character '$'

When I look at the view, it is using string interpolation.

enter image description here

All the projects in this solution are set to target full .Net Framework 4.6.1. From what I read, that should default to C# 7.3 compiler.

I have updated the DomCompiler and Compiler packages to version 3.6.0. In the web.config I tried to set c# version to both default and 7 specifically. The error occurs no matter which one is used.

I also tried to add LangVersion to the .csproj file and specify 7, but that didn't work either.

If we deploy not pre-compiled these views work, so the run time on the server is usually the correct c# compiler version. This is only a dev time and build time issue.

PilotBob
  • 3,107
  • 7
  • 35
  • 52
  • Your issue is somewhere in the .csproj, please post its contents. – Ian Kemp Aug 11 '20 at 16:01
  • It may help if you show the .csproj, also web.config compilers section as well as packages.config. Somewhere the wrong compiler is being used probably the one that comes with .net rather than visual studio. – Andrew Aug 11 '20 at 22:00
  • Also what does csc -langversion:? show when run from visual studio command prompt? This would be the latest lang version used by visual studio although you can target lower ones for the project. – Andrew Aug 11 '20 at 22:03

2 Answers2

0

Visual Studio 2019

Visual Studio 2019 chooses the language version by default:

  1. Right click on your project and select Properties
  2. Choose Build and click the Advanced button
  3. click on Why cant's I select a different version?

enter image description here

The latest C# compiler determines a default language version based on your project's target framework or frameworks. Visual Studio doesn't provide a UI to change the value, but you can change it by editing the csproj file. The choice of default ensures that you use the latest language version compatible with your target framework.

If you want to override the language version you have 3 options:

  1. Manually edit your project file.
  2. Set the language version for multiple projects in a subdirectory.
  3. Configure the -langversion compiler option.

The first option seems to achieve your goal, open the project file in your favorite text editor and add the language version, e.g:

MyProject.csproj

<Project>
    <PropertyGroup>
        <LangVersion>latest</LangVersion>
        <!--<LangVersion>preview</LangVersion>-->
        <!--<LangVersion>7.3</LangVersion>-->
    </PropertyGroup>
</Project>

Visual Studio 2017

  1. Right click on your project and select Properties
  2. Choose Build and click the Advanced button
  3. Here you can choose your Language version

enter image description here

Change language version for all of the projects at one go

If you have several projects in your solution and you want to create a configuration to change the language version for all the projects at one go, then you need to create a file name Directory.Build.props at the root of your repository. You can configure the language version in this file, for example:

Directory.Build.props

<Project>
    <PropertyGroup>
        <LangVersion>latest</LangVersion>
        <!--<LangVersion>preview</LangVersion>-->
        <!--<LangVersion>7.3</LangVersion>-->
    </PropertyGroup>
</Project>

See this question for detailed explanation.

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
  • Are you using Visual Studio Code? – Hooman Bahreini Aug 10 '20 at 23:52
  • No. VS 2019 with .Net framework projects targeting 4.6.1 atm. – PilotBob Aug 11 '20 at 13:46
  • So this is what confused me. The default version for .net 4.6.1 should have been 7.3. But it didn't seem to be using that. I updated the DomCompilers to 3.6.0 and in the web.config rather than langversion=defualt I set it to 7.3. This seemed to solve the problem. There must be something in my solution messing the auto selection up. – PilotBob Aug 12 '20 at 19:59
-2

You can not use c# 7 with .Net framework projects targeting 4.6.1. It does not matter which visual studio version you're using.

Please check this link: https://www.tutorialsteacher.com/csharp/csharp-version-history

I had the same problem (not being able to use all language features) while using (.net 4.5.2)

ANSerpen
  • 323
  • 3
  • 11