简体   繁体   English

基于ReSharper将属性更改为表达式身体会导致错误?

[英]Changing a property to expression-bodied based on ReSharper leads to error?

one of my properties looks like this: 我的一个属性看起来像这样:

public string Name
{
 get{ return _name; }
 set { _name = value; }
}

but ReSharper is advising me to change it to: 但ReSharper建议我将其更改为:

public string Name
{
 get => _name;
 set => _name = value;
}

if I refactor like that then compilation throws error Is it not possible to have expression body in a Property ? 如果我像那样重构那么编译抛出错误是不是可能在属性中有表达式主体?

Before c# 6 you couldn't use expression bodies in properties and had to write something like this. 在c#6之前,您无法在属性中使用表达式主体,并且必须编写类似这样的内容。

public string FullName
{
    get { return string.Format("{0} {1}", FirstName, LastName); }
}

In c# 6 you can create readonly experession bodies. 在c#6中,您可以创建只读的训练体。

public string FullName => $"{FirstName} {LastName}";

In c# 7 you got expression bodies for members like you showed. 在c#7中,你得到了像你所展示的成员的表达主体。

public string Name
{
    get => _name;
    set => _name = value;
}

If you want ReSharper not to adapt this behavior you can change it: 如果您希望ReSharper不适应此行为,您可以更改它:

Resharper > Options > Code Editing > C# > Code Style Resharper>选项>代码编辑> C#>代码样式

and change the following property: 并更改以下属性:

Code body > Properties, indexers and events from Expression body to Accessors with block body 代码主体>从Expression bodyAccessors with block body器的属性,索引器和事件

If you just want to disable the suggestion change the notification state of the property mentioned above. 如果您只是想禁用该建议,请更改上述属性的通知状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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