Get-only or Set-only Property #7760
Replies: 4 comments 4 replies
-
If line count is all you are worried about then this can be done today. float MyProperty { get
{
float result = 0f;
// ...
return result;
}} |
Beta Was this translation helpful? Give feedback.
-
With sequence expressions you could use a normal expression-bodied property without requiring a new form of syntax specific to this purpose. |
Beta Was this translation helpful? Give feedback.
-
Very unlikely to do this, given that a get-only method with statements is already extremely tiny and lightweight. |
Beta Was this translation helpful? Give feedback.
-
If expression blocks were ever implemented (#3086), then your getter example would be expressible as: float MyProperty => {
float result = 0f;
// ...
result
}; But from what I can remember, the language team seemed only interested in expression blocks for switch expressions and even there the interest seemed minimal, so I doubt this will happen. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Sometimes, we want to define properties that only contain either a
get
orset
method, but we cannot achieve this with a single-line expression. Consequently, we have to write nested curly braces, as shown below.In this example, the
get
and its associated pair of curly braces together occupy three lines. Compared to methods, properties with such structure contain more redundancy, while the original purpose of properties is to be simple. But with a small modification, it can be simplified, as shown below.Or like this.
Similarly, set-only properties are also like this. Since get-only properties are more common, the
get
can be further omitted, like this:Beta Was this translation helpful? Give feedback.
All reactions