Expression-bodied property set notation could be further simplified #8722
Replies: 6 comments
-
With generators you won't even need to type the setter at all, [BiDirectional]
public string Label => delegateTo.AString;
// to be generated
public replace string Label
{
get => delegateTo.AString;
set => delegateTo.AString = value;
} Or if it's desirable to delegate all members, [Mixin]
private readonly ComplexObject delegateTo; And let the generator generate code for member delegation. |
Beta Was this translation helpful? Give feedback.
-
But will generators ever realistically happen? In my view, having an expression after the setter, which is converted to |
Beta Was this translation helpful? Give feedback.
-
Nah. |
Beta Was this translation helpful? Give feedback.
-
How would you deal with the fact that code like the following is already valid? string field;
ref string Accessor() => ref field;
public string Label
{
get => Accessor();
set => Accessor();
} (Yes, in this specific case, the code has a bug. But there could be cases where similar code is intentional.) |
Beta Was this translation helpful? Give feedback.
-
Shorter possible notation: |
Beta Was this translation helpful? Give feedback.
-
If the nested class can be private: class ComplexObject
{
public string AString;
}
ComplexObject delegatedTo = new ComplexObject();
public ref string Label => ref delegatedTo.AString; |
Beta Was this translation helpful? Give feedback.
-
The updated expression-bodied property syntax is great, but in my opinion is still a little clumsy regarding setting values. Currently, for a simple read/write property with no other logic, the property looks like this:
I'm aware this can be accomplished with a standard auto-property, so here's a more realistic example with a delegated property:
My issue is with the
= value
part of the statement, which I feel is superfluous because that's what you're going to be writing 99% of the time you're declaring properties in this way. I'd be much happier being able to write:Beta Was this translation helpful? Give feedback.
All reactions