Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

c# - Inline variable declaration not compiling

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.
";
    }
  }
  else
  {
    e += "Expiration Year is not a number.
";
  }

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...