54

I've been getting a message in Visual Studio 2017, specifically, IDE0018 Variable declaration can be inlined.

So I try using an inline variable declaration the way it's mentioned in the visual studio 2017 release notes, but I can't get my project to compile.

It show no error messages, but the output shows "Rebuild All failed..... error CS1525: Invalid expression term 'int'"

The error only shows up in the output, not as an actual error in the error list.

Here is an actual example of the code I'm using that is failing.

if (int.TryParse(ExpYear, out int IExpYear))
  {
    if (IExpYear < DateTime.Now.Year || IExpYear > DateTime.Now.AddYears(10).Year)
    {
      e += "Expiration Year is invalid.\n";
    }
  }
  else
  {
    e += "Expiration Year is not a number.\n";
  }

If I revert the change, it compiles as expected. Is it possible that I'm not using the c#7 compiler somehow?

Thank you.

Update: I found the language setting in Build > Advanced and set it to C# 7.0. Building the project now gives me this error:

CSC : error CS1617: Invalid option '7' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6.

Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
prudan
  • 1,081
  • 1
  • 8
  • 11
  • Your code compiles just fine for me in VS2017RC, both in a .Net Framework and a .Net Core project. Could you describe in what kind of project this is happening and either provide full steps to reproduce this or share your whole project? Also, are you running the latest version of VS2017RC? I think there were some updates to it, which could have effect on this. – svick Jan 24 '17 at 23:08
  • That does suggest that you might somehow be getting the old compiler. Is this only on this one project, or do you see the same thing if you create a brand new project as well? –  Jan 24 '17 at 23:10
  • I'm pretty sure I'm running the latest version of VS 2017 RC, I run the update program daily. I originally started this application in VS 2015, then moved it to VS 2017. When this code didn't work (and some other things), I decided to rebuild it from the ground up and create a new project in VS 2017. The issue remains. – prudan Jan 25 '17 at 15:50
  • The application in particular is a Web API 2 application that is targeting 4.6.2. I couldn't figure out any way to tell what version of the C# compiler I'm using though. It seems that the intellisense thinks I'm running C#7, but the compiler doesn't. – prudan Jan 25 '17 at 15:53
  • I found the option in Build > Advanced to change the language version to C# 7.0. When I did that, I now get this error instead: CSC : error CS1617: Invalid option '7' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6. – prudan Jan 25 '17 at 16:59
  • I would recommend asking a new question about the _ must be ISO-1, ISO-2, Default or an integer in range 1 to 6._ error. This one has another topic and is solved to some extend. Asking a new question will attract new answerers. – Mafii Jan 26 '17 at 11:20
  • That error means something is wrong with the installation. For some reason VS is using the older compiler. Do you have the same problem if you create a *new* project? If yes, you should probably repair your VS 2017 installation – Panagiotis Kanavos Jan 27 '17 at 16:28
  • I thought that there might be an issue with the installation as well. Not only did I create a new project, but I also completely reinstalled VS2017 after the installation tool found nothing to repair. – prudan Jan 30 '17 at 19:01
  • @PanagiotisKanavos - No, I think the installation is fine. Some time ago, I reported an error close to this one ("...must be ISO-1, ISO-2, Default or an integer in range 1 to 6.") to the developer community @ Microsoft, and they think it is **[not a bug](https://developercommunity.visualstudio.com/content/problem/184363/changing-c-language-version-to-c-latest-minor-vers.html)** (which I disagree on). – Matt Apr 26 '18 at 10:56

3 Answers3

43

I was able to resolve this by installing the Microsoft.Net.Compilers nuget package for v2.0.0-rc3, the only version installed prior was 1.3.2.

I still don't understand why the intellisense and compiler errors would show up if the installed compiler didn't support this.

Mafii
  • 7,227
  • 1
  • 35
  • 55
prudan
  • 1,081
  • 1
  • 8
  • 11
  • 3
    You should note that `Microsoft.Net.Compilers` is not supported by Visual Studio For Mac. *P.S. I can't comment cause of reputation, so I only could write an answer.* – isabasan Mar 08 '17 at 09:32
  • For me, updating `Microsoft.Net.Compilers` package resulted in the project running fine locally, but being unable to build ([dynamic compilation](https://msdn.microsoft.com/en-us/library/ms366723.aspx)) on the server I publish to. So just note that there might be multiple steps needed if you want to support it. – Aske B. Oct 26 '17 at 15:29
17

In case the above answer doesn't work for you, as it didn't work for me do the following:

Open the csproj file and check if you have the following package referenced in the file after the upgrade, if yes, remove it.

<Import Project="packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props" Condition="Exists('packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props')" />

Next, check the "Project ToolsVersion". It has to be 15.0, it probably is 14.0 though so you have to change that.

<Project ToolsVersion="15.0" .../>

Then simply reload the SOLUTION and you're good to go. Be aware that if you select "Reload Project" it will give you an error and not load it.

  • 1
    This was the right answer for me, i upgraded from VS 2015 to 2017, and also had Compilers 1.3.2 installed in the past. There was still a reference to it in the csproj, even after upgrading to 2.0.1 – Paul Becker Mar 23 '17 at 11:16
  • yes did it for me too. i had a huge mess in my csproj file. thanks! – gsharp May 17 '17 at 15:17
  • 1
    Strangely, I tried that and got **[this other issue](https://stackoverflow.com/q/32523580/1016343)** afterwards. Changed tools version from 12.0 to 15.0 and removed the import. – Matt Oct 23 '17 at 12:14
  • 1
    In our case, beside changing **ToolsVersion** from **14.0** to **15.0**, on project file (**.csproj**), we also had to install an updated version of **MSBuild**, since compilation is being triggered my a **Jenkins** job. After installing **Build Tools For Visual Studio 2019**, we got MsBuild version 16.0, upon which all new C# features compiles succesfully. – Julio Nobre May 06 '19 at 16:16
0

To set the use of latest released C# compiler:

In Visual Studio, (I'm using Visual Studio 2017), right click project and select "Properties"

Select the "Build" tab in left-side menubar

Click "Advanced..." button in lower right corner of the "Build" window

Under "General," in listbox to the right of "Language Version", select "C# latest minor version (latest)"

Also, make sure to keep the Visual Studio IDE up-to-date using Visual Studio Installer.

BoiseBaked
  • 792
  • 7
  • 15