Using "using" to map auto-implemented properties to a value source #2291
Replies: 4 comments
-
So you just want 2 way property |
Beta Was this translation helpful? Give feedback.
-
@johnkellyoxford public int Foo
{
get => x / 10;
set => x = value * 10;
} Can be reduced to: public int Foo {get; set;} using x/10; It will need a little work to calculate the correct formula of the setter, as |
Beta Was this translation helpful? Give feedback.
-
I don't like applying a new meaning to the using keyword. From the syntax above it just confuses me and makes me think that your source implements IDisposable, which would make it impossible to infer when the Dispose method would even be called. Here is a syntax I think would look a lot better: public int NewYork { get; set; } => this["NewYork"]; The get and set would need to be in the declaration in order for the compiler to infer that the property also sets. But, honestly I don't think there is much value in this. Nothing substantial is solved or alleviated from developers with this kind of change. |
Beta Was this translation helpful? Give feedback.
-
I really don't think this is very useful But if it was, I'd go with a syntax something like:
Adding a new meaning to using is just conf using |
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.
-
I suggest to use
using
to map auto-implemented properties to a variable, a filed or an indexer... etc.Syntax:
Which is a shortcut for:
This also allows:
Which is a shortcut for:
which gives you access to the backing field _age.
Most of the time, we write a full property, just to link it directly to an item of a collection or a property of a control. Example:
Can be reduced to:
It will need a little work to calculate the correct formula of the setter, as
x/10 = value
=>x = 'value * 10
the same for using x - 2:
x-2 = value
=>x = 'value + 2
and Math.Pow(x, 2)
Math.Pow(x, 2) = value
=>x = 'Math.Pow(value , 1/2)
We can set some simple roles like this, and any more complex relations should be written in a full property syntax.
Beta Was this translation helpful? Give feedback.
All reactions