Yes. I take that point. I still think it's a bit weak, though. That
argument is fine for public setters, because the value is being set by
people who may not understand the domain of possible values for the
property. With a private set{}, the value is being set by the person who
determines that domain - i.e. the person coding the class itself.
It sounds as though you are considering only error checking. I don't
think Christof meant to limit his comment to that scenario, and for sure
that's not the only reason a setter would have extra code in it. His
point about evolving code is good too. In addition to the examples I
offered in another post, it's also true that the way a private class
member is used could change over time, and restricting access to be
through a property would help allow that change to occur without having to
do a massive rewrite to the rest of the code.
(It doesn't get you out of having to at least visit and look at every use
of the member and related property -- double-checking is critical for
avoiding bugs -- but at least the goal would be that you only have to
*look*, and not write a bunch of new code everywhere that the member is
used).
Of course, there's no way of enforcing the use of a private set{},
either. Is there? Or have I missed something?
What do you mean "enforce"? It's true that even if the setter is set to
"private", there's nothing to stop code elsewhere in the class from still
accessing the underlying storage directly. But that would be a bug,
assuming the property was written for the purpose of encapsulating that
underlying storage. One hopes that anyone maintaining the code would be
aware of that and would use the property as intended.
Of course, there's no guarantees of that...perhaps that's a good argument
for inclusion of some way to restrict access of a class member to *only*
from within a property. For all the hand-holding that C# does, it's a
little surprising that they don't include something like that.
But none of that means that there's no reason to have a private setter.
BTW, I wouldn't want to start a war over this. I'm just curious as to
the justification.
Before I saw all this, I posted a different post in reply to your original
that I hope includes enough other examples of how a private setter is
useful. Please feel free to ask for clarification if you're still
wondering.
Pete