Note: Recommendations: an exception to properties to explain and discuss why throwing exceptions to properties is bad.
Admittedly, this post talks about property assets.
Setters usually see consumers as simply setting a private field hidden by this property, and perhaps doing some extra things as needed, exceptions are unexpected behavior, and can you imagine what each set statement needs to be enclosed inside a try block?
Although this can be taken as a guide, this is a guessing nightmare for developers, and the validation logic should be contained in a separate method and then called from the property, if necessary.
If you need validation or special behavior when setting properties, you should use the set method, for example. SetMyProp(string value) , since it makes a difference for it, that this can lead to an exception, etc.
If you use properties for something like a WPF model, you should use the built-in WPF validation for the data instead.
Clint
source share