Property Observers in C# like Swift #604
Replies: 8 comments 2 replies
-
Why is this an improvement on writing that logic in the setter/getter methods? It's not like you can observe the changes of other instances, so it doesn't cover the same use cases as |
Beta Was this translation helpful? Give feedback.
-
I'd much rather have the public int Value
{
get;
set
{
Console.WriteLine($"Total Counter is: {value}");
var oldValue = field;
field = value;
if (counter > value)
Console.WriteLine($"Newly Added Counter {counter - oldValue}");
}
} Or even in this case: public int Value
{
get;
set
{
Console.WriteLine($"Total Counter is: {value}");
if (counter > value)
Console.WriteLine($"Newly Added Counter {counter - field}");
field = value;
}
} |
Beta Was this translation helpful? Give feedback.
-
I don't like |
Beta Was this translation helpful? Give feedback.
-
I like the idea of the of the private IEnumerable<EmployeeViewModel> _Employees;
public IEnumerable<EmployeeViewModel> Employees
{
get => _Employees;
set => SetField(ref _Employees, value);
} With the field keyword this could become: public IEnumerable<EmployeeViewModel> Employees
{
get;
set => SetField(ref field, value);
} With proposal #318 it would look like that: public IEnumerable<EmployeeViewModel> Employees
{
IEnumerable<EmployeeViewModel> field;
get => field;
set => SetField(ref field, value);
} You still have to repeat the type for the backing field and you have to declare the backing field. I definitely prefer @jnm2 proposal. The only problem I see, is that the |
Beta Was this translation helpful? Give feedback.
-
Yes it is breaking change. That's what I said 318 is enough. Stop introducing new keyword just for lazy purpose. And also there are many possibility to work with property by more than one field. Or even using many property on one field. field keyword has just only one usage. It not worth for being keyword |
Beta Was this translation helpful? Give feedback.
-
I think this would be a good change. willSet and didSet are useful. There will always be a lot of people who drag their feet on language features or syntax improvements so they don't have to learn anything else their whole lives. Don't let others get you down about it. |
Beta Was this translation helpful? Give feedback.
-
Now that I picked up C# after working in Swift for many years , this is the one language feature I miss most. (That and proper enum support of course)
vs
TL;DR: Please give use a |
Beta Was this translation helpful? Give feedback.
-
In the same way that INotifyPropertyChanged can use source generators and an attribute, I reckon this could, too. [DidSet(nameof(UpdateVisibility))] |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In Swift to observe and respond to property values Property Observers are used. Each and every time when property values are set property observers are called. Except lazy stored properties we can add property observers to 'inherited' property by method 'overriding'.
Property Observers can be defined by either.
When a property is set in an initializer willset and didset observers cannot be called.
This can be more useful than IPropertychanged and I think can be a very very small way to gathering AOP programming based on C# syntax.
Beta Was this translation helpful? Give feedback.
All reactions