Any reason to use auto-implemented properties over manual implemented properties?

前端 未结 7 1314
Happy的楠姐
Happy的楠姐 2020-11-27 07:22

I understand the advantages of PROPERTIES over FIELDS, but I feel as though using AUTO-implemented properties over MANUAL implemented properties doesn\'t really provide any

7条回答
  •  爱一瞬间的悲伤
    2020-11-27 07:32

    It doesn't grant you anything extra beyond being concise. If you prefer the more verbose syntax, then by all means, use that.

    One advantage to using auto props is that it can potentially save you from making a silly coding mistake such as accidentally assigning the wrong private variable to a property. Trust me, I've done it before!

    Your point about auto props not being very flexible is a good one. The only flexibility you have is by either using private get or private set to limit scope. If your getters or setters have any complexity to them then the auto props are no longer a viable option.

提交回复
热议问题