Best Practice on local use of Private Field x Property

后端 未结 9 1715
萌比男神i
萌比男神i 2021-01-22 02:09

When inside a class you have a private fiels and expose that field on a public property, which one should I use from inside the class?

Below you is an example on what I

9条回答
  •  误落风尘
    2021-01-22 02:48

    Original poster is EXACTLY correct.

    1) Access your private fields directly.

    • Makes refactoring easier.

    2) If accessing accessors use the keyword ME. to improve readability

    • explicitly listing scope requires less thinking by reader

    3) Use the accessor only if it implements vital logic logic that also applies to private access. This way you know that if you are using the accessor it is because there is “something else to it”

    • this is the only reason to violate rule #1.

    4) Avoid using Protected Fields. Derived classes should always use the accessor, never direct access to the field.

提交回复
热议问题