2

When implementing simple properties, you can make use of auto-implemented properties, to make the code look simpler, like this:

public int MyInt
{
    get; set;
}

Or you can implement it with a "manual" private field. When this is the case, I have often seen people checking for value changes before using set. Like this:

private int _myInt;

public int MyInt
{
    get { return _myInt; }
    set 
    {
        if value != _myInt
            _myInt = value;
    }
}

So my question is this: Is checking for the changed value (in the latter case) good for anything? And if it is, then what impact does this have on the auto-implemented properties?

Jakob Busk Sørensen
  • 5,599
  • 7
  • 44
  • 96

4 Answers4

3

You would use that check when you implement INotifyPropertyChanged
(You want to fire an event only when the value of the property actually changes, not simply when the setter is called.)

Dennis_E
  • 8,751
  • 23
  • 29
  • +1 but you may want to delete the last sentence. It can be useful in some other cases as you can see from the other answers. – Racil Hilan Jun 20 '17 at 08:59
  • While I have marked this answwer as accepted, I strongly suggest to read all answers. There is a lot of great points to be found in this post. Thank you to everyone who participated. – Jakob Busk Sørensen Jun 30 '17 at 06:19
1

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).

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • 1
    You probably should read the question more carefully. Your answer seems to have nothing to do with the question. – Racil Hilan Jun 20 '17 at 08:13
  • @RacilHilan The question is based on a whrong assumption which is that auto-implemented properties can be used in conbination with validation. I cleared that and showed that they actually are just methods. – MakePeaceGreatAgain Jun 20 '17 at 08:15
  • Hmmm, I've read the question again and failed to see any possible wrong assumption. Maybe I don't see what you see. – Racil Hilan Jun 20 '17 at 08:28
  • @RacilHilan And what about "And if it is, then what impact does this have on the auto-implemented properties"? This implies that we can use validation together with AI-properties, doesn´t it? – MakePeaceGreatAgain Jun 20 '17 at 08:31
  • Not necessarily. But possibly. I admit that that sentence is vague and not sure what the OP meant to ask. Let's see if the OP will comment on your answer. Anyway, you still didn't answer the main question, so your edit was good for that. – Racil Hilan Jun 20 '17 at 08:44
  • It did address my main point, which was related to how auto-implemented properties work behind the curtain. I might not have been clear enough regarding the second point, but my interest was in the special case of validation, where the value has not changed. Not validation in general. Very informative post though. So thank you! – Jakob Busk Sørensen Jun 20 '17 at 08:52
1

Is checking for the changed value (in the ladder case) good for anything?

Not really, at least not in the example you provided. Well, unless you have code that assigns to the property in a huge loop (hundred thousands), in which case, if checking the value is milliseconds faster than the assignment, then you will see a noticeable difference. I said "if", because I'm not sure it is the case for int, so you'll have to test it if you're interested in the answer.

However, in other scenarios, there could be a noticeable difference. For example, if the setter is more than just assigning the value.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
1

Lets assume MyInt is X location of your mouse, you have a infinite loop that takes mouse location every milisecond, and shows on a window. If you do not check you have to update window every miliseconds, when your mouse is standing still.

Most of the cases you do not need to check, even if it is the same value, value is changed. Do it when you have performance issues.

tetralobita
  • 453
  • 6
  • 16
  • 1
    That's a good example, but you're assuming that the assignment is slower than checking. I'm not sure that's the case, at least not for simple types. But if the property is of a complex type and the checking is only reading part of it, then this can be an excellent example. – Racil Hilan Jun 20 '17 at 08:35
  • Great example. Could you point out (for the sake of other people reading this) that the required validation for this example means that you should use the "manual" approach? – Jakob Busk Sørensen Jun 20 '17 at 08:42