简体   繁体   中英

Expression bodied get / set accessors feature in c# 7.0

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?

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM