Is there a way to use INotifyPropertyChanged
with auto-properties? Maybe an attribute or something other, not obvious to me.
public string Demo{ get;set; }
For me, auto-properties would be an extremely practicall thing, but almost always, I have to raise the PropertyChanged
-event if the property value has been changed and without a mechanism to do this, auto-properties are useless for me.
Auto-implemented properties declare a private instance backing field, and interfaces may not declare instance fields. Declaring a property in an interface without defining a body declares a property with accessors that must be implemented by each type that implements that interface.
The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .
properties allow your access to be polymorphic (inheritors can modify access if the property is virtual) if you so choose. auto-properties are nice when you're dealing with simple get/set operations. if you do more complicated operations inside your get / set, then you can't use the auto-property.
11 Answers. Show activity on this post. Automatic Properties are used when no additional logic is required in the property accessors.
In .NET 4.5 and higher it can be made somewhat shorter:
private int unitsInStock; public int UnitsInStock { get { return unitsInStock; } set { SetProperty(ref unitsInStock, value);} }
It's something you would have to code yourself. The closest you could get would be something like this implementation on Code Project that uses a custom attribute and aspect orientated methods to give this syntax:
[NotifyPropertyChanged] public class AutoWiredSource { public double MyProperty { get; set; } }
Someone once proposed on Microsoft Connect a change to the C# specification implement this:
class Person : INotifyPropertyChanged { // "notify" is a context keyword, same as "get" and "set" public string Name { get; set; notify; } }
But the proposal has been now closed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With