Inline variable declaration not compiling

C#.Net CoreVisual Studio-2017C# 7.0

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.

Solution 3 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprudanView Question on Stackoverflow
Solution 1 - C#prudanView Answer on Stackoverflow
Solution 2 - C#DomView Answer on Stackoverflow
Solution 3 - C#BoiseBakedView Answer on Stackoverflow