Expression-bodied properties vs. {get; set;} [duplicate]

社会主义新天地 提交于 2020-01-10 10:22:17

问题


When I have Visual Studio 2017 generate properties for me, it will always use the new expression-bodied properties, for example:

private static string username;
internal static string Username { get => username; set => username = value; }

Is there any advantage of using this style over the following or is it just a matter of preference and readability?

internal static string Username { get; set; }

回答1:


Expression-bodied syntax is convenient to use it in the following cases:

Get or Set only property

public DateTime Date => DateTime.Now;

Methods

public IEnumerable<string> GetData => SomeMethodThatReturnData.Select(x => x.Name);

And constructor with 1 input parameter

public SomeClass(IRepository repository) => _repository = repository;



回答2:


Yes, there is a difference; a big one actually. Your former solution will create an infinite loop because the property getter and setter is referencing itself.

string Username { get => Username; set => Username = value; }

This is strictly equivalent to the following (since it’s just syntactic sugar):

string Username
{
    get
    {
        return Username;
    }
    set
    {
        Username = value;
    }
}

So the getter and setter for the property Username both reference the member Username which is itself. So when you access the member, it will repeatedly call itself forever and never get back a result.

You probably meant to do the following:

string _username;
string Username { get => _username; set => _username = value; }

Now you have a backing field which you reference to actually store the value of the property. This works fine and there is no difference to the more verbose getter and setter syntax. It compiles to the same thing; it’s just that C# 6 allows you to make it a bit simpler here.

The remaining difference is the explicit backing field vs. the backing field that is automatically created when using an auto-property. Whether you want to use auto property depends a bit on your style, but in general, there is no real reason not to use them if you’re going to create the same backing field otherwise manually. Of course, if you are not storing the value in a plain backing field and/or need additional logic, then you of course cannot use auto properties here. But then you probably don’t end up with expression-bodied properties anyway. See also this question on auto properties.




回答3:


A few advantages that I can see quickly in the second style:

  1. You type less.
  2. Can be read quickly.
  3. Doesn't make reader think that the getter or setter are doing something special.
  4. Find All Reference returns fewer results.
  5. Expression evaulator is not triggered by the compiler.

Edit

I missed one major problem with the first style. See poke's answer below.



来源:https://stackoverflow.com/questions/44044891/expression-bodied-properties-vs-get-set

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