简体   繁体   中英

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:

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.

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

In c# 6 you can create readonly experession bodies.

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

In c# 7 you got expression bodies for members like you showed.

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

If you want ReSharper not to adapt this behavior you can change it:

Resharper > Options > Code Editing > C# > Code Style

and change the following property:

Code body > Properties, indexers and events from Expression body to Accessors with block body

If you just want to disable the suggestion change the notification state of the property mentioned above.

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