12

I'm having this code in a class

private string test;
public string Test
{
    get =>  test;
    set => test = value;
}

But the compiler won't let me compile. It says

CS1043 { or ; expected 
CS1513 } expected

I'm using VS 2017 and targeting .NET FW 4.6 with a MVC 5 Project

Any idea why it isn't working?

gsharp
  • 27,557
  • 22
  • 88
  • 134

3 Answers3

13

You have to set the compiler to version 7 in your project.

Project properties → (tab) Build → Advanced → Language version = C# 7.0

UPDATE BY @gsharp

check also your (NuGet) reference to the .NET Compiler platform Microsoft.Net.Compilers.

gsharp
  • 27,557
  • 22
  • 88
  • 134
Igor
  • 60,821
  • 10
  • 100
  • 175
  • 1
    Thanks for your answer. That Issue happened in a project at work and now I'm home and can't evaluate it. Hover I've tried to reproduce it at home. Started a project in 2015 opened it in 2017. Guess what? It worked :-p. Then I've changed the Language version to 6.0 and here I get a proper Compile Error "Feature is not available in c#6 use 7". Can't wait to get back to work on Tuesday :) – gsharp Apr 28 '17 at 17:07
  • That's odd - I haven't had to do this for any of mine. All of my projects are set to "default" language version and worked with C# 7.0 as soon as I upgrade to 2017. – BJ Myers Apr 29 '17 at 03:13
  • In my case it was the "old" the .NET Compiler platform referenced that caused the problem. – gsharp May 02 '17 at 07:52
0

I had the same problem. I have compared my project to another project that was OK and found this line from .csproj cause the problem.

<Import Project="$(MSBuildToolsPath)\Workflow.Targets" />

Simply remove it and it worked and don't know why.

Iman Bahrampour
  • 6,180
  • 2
  • 41
  • 64
0

The answer marked above is the working solution for me. I could not add comments with my current points but i thought of sharing few more information for others who may find similar situation like me.

In my case, we are using Teamcity builds to run the solution and it was running (MS Build step) using Microsoft Visual Studio 2015. Luckily we also have the Microsoft Visual studio 2017 in the Visual studio section of the build step. I selected 2017, and it ran successfully.

For anyone who is using either TeamCity or Jenkins or similar, first thing is to ensure if your tool has the latest MS build/VS build version configured, like 2017 or 2019 (to be latest). If not, get that first and then point to that version.

There's another hack/option. Tools like JEnkins, Teamcity can also have a step to install the nugets and this post may help too to establish that.

Ak777
  • 346
  • 7
  • 18