Benefits of Propeties
It's generally best practice to encapsulate our fields with properties.
But, why that is?
But, why that is?
With properties,
- Fine-grained access control, If a field is public any code can get or set the field, with the property, we can define separate accessibility for the getter and the setter.
- For example, the getter here is internal and the setter is private so only the code within the same component can see the property and code within the same class can set it.
- in the second example, the getter is public and the setter is internal, so any code get access to the property, but only code within the code can set it.
- Execute code, We can execute code before returning the value or before setting the value.
- We can check user credentials or application state.
- Format or convert the value.
- perform validation.
- When debugging we can set a breakpoint on the property to see when the property is changing, or when we can add logging.
- Available for data binding.
Comments
Post a Comment