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; }
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;
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.
A few advantages that I can see quickly in the second style:
I missed one major problem with the first style. See poke's answer below.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With