With the new approach of having the get/set within the attribut of the class like that :
public string FirstName {
get; set;
}
Why simply not simply put the attribute FirstName public without accessor?
Two of the big problems with direct access to variable inside class (field/attribute) are:
1) You can't easily databind against fields.
2) If you expose public fields from your classes you can't later change them to properties (for example: to add validation logic to the setters)
Because, in the future, if you change the implementation, code using the current interface won't break.
For instance, you implement a simple class with a public field and start using your class in some external modules. A month later you discover you need to implement lazy loading in that class. You would then need to transform the field to a property. From the external module point of ciew, it might look the same syntaxicaly, but it is not. A property is a set of functions, while a field is an offset in a class instance.
By using a property, you effectively reduce the risk the interface will change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With