Expression bodied get / set accessors feature in c# 7.0

旧巷老猫 提交于 2019-12-18 07:36:33

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/43681882/expression-bodied-get-set-accessors-feature-in-c-sharp-7-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!