get/set Expression bodied properties #1007
Replies: 19 comments 4 replies
and returns...? |
|
I initially upvoted this as a simple idea for mapping read/write properties to some value other than the default backing field. But I've removed that upvote as your "Notes to consider" addendum just muddies the proposal. Keep it simple:
|
|
@DavidArno More explicit on the indexer? i.e. must always resolve to a single value/ref |
|
@Logerfo returns what @DavidArno indeed, i tried to expand usage in conjunction with get only properties. Chained assignments could be prevented. Its not something i do usually, in fact i never did it. |
|
@benaadams i liked that but what if item is jagged array Then it has to be either explicit like what you suggest or implicit I guess explicit version pays off more. |
|
@MkazemAkhgary in C#, multiple assignments returns the rightmost value of the chain. In your examples, |
Defined via type, an array is still a single item private int[][] item;
public int[] this[int index] <=> item[index];or private int[][] item;
public int this[int i1, int i2] <=> item[i1][i2];or the other type private int[,] item;
public int this[int i1, int i2] <=> item[i1, i2]; |
|
I think it just needs to be "expression must be a valid l-value and r-value" i.e. valid on both sides of an equals For example it could happily be a private int[] items;
private ref int FindEntry(int index)
{
// ...
}This public int X
{
get => FindEntry(0);
set => FindEntry(0) = value;
}
public int this[int index]
{
get => FindEntry(index);
set => FindEntry(index) = value;
}Could be converted to public int X <=> FindEntry(0);
public int this[int index] <=> FindEntry(index); |
|
Another possibility is to use just the |
Would be a significant breaking change as all current simple readonly properties would suddenly become read/write e.g. private int _x;
public int X => _x; // now read/write, was readonly before |
|
@benaadams Since the assignment is not possible today, that change would not actually break anything, as it's more kind of a relaxation. It can break analyzers, but anything can. I think it would only break reflection/cecil for properties like |
|
@Logerfo not all properties can become writable. issue is they will remain get only or become writable. so whether property will be readonly or not would become vague. I guess for example |
|
@Logerfo hmm true; but it would also mean everyone who had written a readonly property would then have to change/audit their code to keep the properties readonly |
|
@benaadams very good explanation. that is more general, updated. |
|
I suggest public int this[int index] using _source[index]; |
|
That syntax deviates too much from |
|
Maybe extend the public int this[int index] ref _source[index];
public int Prop ref _field; |
|
I know the public string Prop { get, set => this._field; } |
Alternative 1
where
Alternative 2
I'm in full support of alternative 1, and somewhat supportive of alternative 2. I'm per se not a fan of "complex" expressions on the RHS like
I know it's already possible to write but I am a bit worried that people might read the shorthand syntax as
and not realize that Indexer expressions are on the boundary of "complex" RHS expressions, because they take additional arguments. But seeing that indexer delegation is probably often required alongside property delegation, if the wrapped/delegated object happens to have an indexer property, I'm slightly less worried about that. |
Uh oh!
There was an error while loading. Please reload this page.
Allow to get and set with expression Bodied properties
Equivalent to
Could be used for indexers too.
Equivalent to
Notes to consider
All reactions