1

I'm getting an object from ViewData.Model in an ActionFilter, and I need to make sure it conforms to a given interface. Here's an example:

object d = DateTime.Now;
IComparable c = d as IComparable;
if (c != null)
{
    //do stuff
}

Visual Studio 2017 gives me an IDE0019 "Inline temporary variable" suggestion on the second line. Selecting it refactors the code to this:

object d = DateTime.Now;
if (d is IComparable c) // Line 31
{
    //do stuff
}

I like this syntax, and I'd like to use it. There are no errors shown in the editor, but when building I get a litany of errors:

1>C:\Users\me\MyProject\MyFile.cs(31,34,31,35): error CS1026: ) expected
1>C:\Users\me\MyProject\MyFile.cs(31,35,31,36): error CS1002: ; expected
1>C:\Users\me\MyProject\MyFile.cs(31,35,31,36): error CS1513: } expected

Line 31 is the if statement. It looks like the compiler is expecting a lot more syntax, but I can't figure out what. The generated code appears identical to this example from the documentation for is:

public int CompareTo(Object o)
{
    if (o is Employee e)
    {
        return Name.CompareTo(e.Name);
    }
    throw new ArgumentException("o is not an Employee object.");
}

How can I fix these errors? I'm on VS2017 (26430.14) and .NET Framework v4.0.30319.

Undo
  • 25,519
  • 37
  • 106
  • 129
  • Existing project or new? Updated all packages? Does it actually run after compiling? Do you have ReSharper? Does the project use the C# 7 compiler (Properties -> Build -> Advanced)? – CodeCaster Jul 06 '17 at 21:52
  • @CodeCaster Existing ASP.NET MVC project. The build fails and the error appears in the build output, but not in the error list. Checking on packages now... – Undo Jul 06 '17 at 21:53
  • @CodeCaster Properties - Build - Advanced on my startup project shows "default" as the language version - not sure what that means. Unsure whether I have ReSharper. Where would I look? – Undo Jul 06 '17 at 21:57
  • @Undo If you used ReSharper then you'd know you have it (costs some hundreds, so...) Try to set to build with c# version 7 in the Build options – Camilo Terevinto Jul 06 '17 at 21:58
  • @CamiloTerevinto I'm on Enterprise, so if it's bundled I may have it and don't know about it. I don't have a reason to think I *do*, though. – Undo Jul 06 '17 at 21:59
  • No, it's a third party software, installed separately – Camilo Terevinto Jul 06 '17 at 22:00
  • ReSharper (Tools -> Extensions) can sometimes show errors which aren't errors, especially for C# 7 and .NET Core, so despite those showing in the Error List (which they don't for you), your project may run fine. For the other remark, there are packages (don't know which atm) that offer compile-time features. Also, the "default" C# version should correspond to the one shipped with the IDE, but it wouldn't harm forcing it to C# 7 just to test. The fact that you see the errors in the build output would lead me to think VS is using an earlier C# compiler. Anything custom in the .csproj? – CodeCaster Jul 06 '17 at 22:04
  • 1
    @CodeCaster Turns out I needed to update the Microsoft.Net.Compilers package to something decently modern (was on 1.0.0). Installing 2.2.0 fixed the issue. Interesting the VS shows the option to use C# 7 even when you don't actually have the compiler for it. You (or Camilo) are welcome to turn that into an answer, it was your idea. – Undo Jul 06 '17 at 22:05
  • 1
    @Undo I think it's the other way around. VS2017 ships with the C# 7 compiler (doesn't it?), but if your project has that package, the "default" compiler setting will cause VS (or MSBuild, rather) to use the compiler from that package. – CodeCaster Jul 06 '17 at 22:12

1 Answers1

3

As found on VS 2017 - New C# 7 feature for out parameters causes project not to build:

Updating Microsoft.Net.Compilers package solved the problem on my asp.net mvc project.

From What is the Purpose of Microsoft.Net.Compilers?:

The point of the Microsoft.Net.Compilers package is that the compilers distributed with that package will be used for compiling your project, rather than the compiler that comes with .NET Framework or with Visual Studio.

So if you're upgrading an older project to use C# 7 features, and the project references that package, make sure to update that package as well, otherwise the compiler contained in that package will be used if the project is set to the "default" compiler.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272