2

In Visual Studio 2017 we have a C#, ASP.NET, MVC project that fails to build when we inline the out variable declaration for int.TryParse, as suggested by Intellisense. The project successfully builds when we declare the variable before executing int.TryParse. We have Visual Studio 2017 version 4.7.02053. The Target Framework is 4.5.2.

This pattern builds in VS 2017.

int consumerID = 0;
int.TryParse(model.TextConsumerID, out consumerID);

This pattern fails to build in VS 2017.

int.TryParse(model.TextConsumerID, out int consumerID);

We use the first pattern in multiple places in the application. When we inline any of the int.TryParse declarations, per the Intellisense suggestion, then Visual Studio fails to build and shows 0 Errors and 0 Warnings. If I have any classes opened that contain other int.TryParse commands that are not inlined, then the following Messages will appear: "Variable declaration can be inlined."

Both patterns should work.

The "Treat warnings as errors" option is set to "None" under the project Properties > Build tab. There is no visible option for treating Messages as errors.

Is this a Visual Studio 2017 bug, or is there some build setting that I need to adjust to fix this? Thanks.

Ken Palmer
  • 2,355
  • 5
  • 37
  • 57
  • 5
    The version of visual studio is not as important as the .NET Framework version your project is targeting. Your second example was not valid until C# 7 – maccettura Aug 27 '18 at 16:11
  • 1
    What's the target framework version for the project? – itsme86 Aug 27 '18 at 16:11
  • Thanks, I added that to the question. – Ken Palmer Aug 27 '18 at 16:13
  • In the project settings, under advanced, what is the language settings value set to? – Lasse V. Karlsen Aug 27 '18 at 16:14
  • Install latest version of C# – Rudresha Parameshappa Aug 27 '18 at 16:14
  • 1
    Not an exact dup, but the same question about a different C# 7 feature: https://stackoverflow.com/questions/42482520/does-c-sharp-7-0-work-for-net-4-5 – itsme86 Aug 27 '18 at 16:15
  • https://dailydotnettips.com/choosing-the-c-language-latest-version-minor-release-in-visual-studio-2017/ – Rudresha Parameshappa Aug 27 '18 at 16:16
  • You say "fail to build", can you be explicit and tell us the compiler error? – Lasse V. Karlsen Aug 27 '18 at 16:16
  • Language version: C# latest major version (default). In the Output dialog this error appears: "error CS1525: Invalid expression term 'int'". But 0 errors appear in the Error List. – Ken Palmer Aug 27 '18 at 16:18
  • If it lists 0 errors or warnings, how do you know that it fails to build? – Lasse V. Karlsen Aug 27 '18 at 16:19
  • Because when I launch the site the following dialog appears. "There were build errors. Would you like to continue and run the last successful build?" – Ken Palmer Aug 27 '18 at 16:21
  • 1
    Ah, so this is not a Visual Studio problem, this is a ASP.NET MVC dynamic compilation problem. Let me find the solution to that. – Lasse V. Karlsen Aug 27 '18 at 16:21
  • I mean, if there are "build errors," then they should show up in the Error List. And if inlining the declaration for int.TryParse doesn't work in the target framework and language that we selected in the solution, then Intellisense shouldn't suggest it. Right? – Ken Palmer Aug 27 '18 at 16:22
  • Thanks for looking into this. Appreciate all the help everyone. :) – Ken Palmer Aug 27 '18 at 16:23
  • The problem here is that the actual compilation happens at runtime using an older compiler. Depending on which version of ASP.NET, .NET Core, etc. you're using different solutions may apply. Unfortunately I cannot find a good duplicate for C# 7 so I am not going to vote to close it, but here's one for an older version of C# - https://stackoverflow.com/questions/35686990/feature-interpolated-strings-is-not-available-in-c-sharp-5-please-use-languag/44025705#44025705 - the same solution should apply, try installing the DotNetCompilerPlatform nuget package. – Lasse V. Karlsen Aug 27 '18 at 16:25
  • Lasse, do you agree this is a bug with VS then? 1. Intellisense suggests code that breaks. 2. No error shows up in the error list tab. – Ken Palmer Aug 27 '18 at 16:38

0 Answers0