You can´t do both, use an auto-implemented property and check its value. As soon as you introduce any logic into either the getter or the setter, it´s not any more an auto-implemented property.
The auti-implementing property is just a syntactic sugar for a getter/setter accessing a private backing-field as if you´d write this:
public int MyInt
{
get { return _myInt; }
set { _myInt = value; }
}
When you want to check if the new value even is valid you have to use a setter with some logic within. For example you may want to check if your integer is positive because your application can´t handle negative values. So you simply introduce that check in the setter.
set
{
if (value >= 0)
_myInt = value;
}
As an aside you can´t define just one part (that is either the getter or the setter), you have to define both with braces. On the other side you can of course completely omit one of them, making your property readonly or write-only (although I can barely think of any use of the latter). Actually even properties are syntactic sugar for a get- and a set-method. So you can think of them as usual methods. Why shouldn´t you introduce any logic there? Of course properties tend to make people think that nothing special is happening within their setter or getter, and this is also what conventions suggest. However there´s actually nothing special on them except the fact that on auto-implementing properties your just accessing a backing-field which you can´t access in your code as it´s name isn´t known to the IDE, however it exist within the IL as shown here.
EDIT: To come back to your question. It won´t make a real difference if you´d introduce the logic for validation of the new value within the setter or before it, thus you can also write this:
int newValue = -1;
if(newValue >= 0)
myInstance.MyInt = newValue;
But this could be quite messy and in particular breaks the DRY-priniciple as soon as you´d need to set the property somewhere else in your code and check it again. So it´s better to do it once, and only once (within the setter).
INotifyPropertyChanged
, Otherwise, it's pretty useless. – Dennis_EINotifyPropertyChanged
. – Jakob Busk Sørensen