Why is this:
public string Foo {get;set;}
considered better than this:
public string Foo;
I can’t for the life of me work it out. Can anyone shed some light?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because you can transparently (from client code’s perspective) change the implementation of the setter/getter wheras you cannot do the same, if you expose the underlying property directly (as it would not be binary compatible.)
There is a certain code smell associated with automatic properties, though, in that they make it far to easy to expose some part of your class’ state without a second thought. This has befallen Java as well, where in many projects you find
get/setXxxpairs all over the place exposing internal state (often without any need for it, “just in case”), which renders the properties essentially public.