get/set Expression bodied properties #1007
Replies: 18 comments 3 replies
-
and returns...? |
Beta Was this translation helpful? Give feedback.
-
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:
|
Beta Was this translation helpful? Give feedback.
-
@DavidArno More explicit on the indexer?
i.e. must always resolve to a single value/ref |
Beta Was this translation helpful? Give feedback.
-
@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. |
Beta Was this translation helpful? Give feedback.
-
@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. |
Beta Was this translation helpful? Give feedback.
-
@MkazemAkhgary in C#, multiple assignments returns the rightmost value of the chain. In your examples, |
Beta Was this translation helpful? Give feedback.
-
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]; |
Beta Was this translation helpful? Give feedback.
-
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); |
Beta Was this translation helpful? Give feedback.
-
Another possibility is to use just the |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
@benaadams very good explanation. that is more general, updated. |
Beta Was this translation helpful? Give feedback.
-
I suggest public int this[int index] using _source[index]; |
Beta Was this translation helpful? Give feedback.
-
That syntax deviates too much from |
Beta Was this translation helpful? Give feedback.
-
Maybe extend the public int this[int index] ref _source[index];
public int Prop ref _field; |
Beta Was this translation helpful? Give feedback.
-
I know the public string Prop { get, set => this._field; } |
Beta Was this translation helpful? Give feedback.
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
Beta Was this translation helpful? Give feedback.
All reactions