Should you ever use protected member variables?

后端 未结 10 1756
星月不相逢
星月不相逢 2020-12-02 07:53

Should you ever use protected member variables? What are the the advantages and what issues can this cause?

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 08:18

    For detailed info on .Net access modifiers go here

    There are no real advantages or disadvantages to protected member variables, it's a question of what you need in your specific situation. In general it is accepted practice to declare member variables as private and enable outside access through properties. Also, some tools (e.g. some O/R mappers) expect object data to be represented by properties and do not recognize public or protected member variables. But if you know that you want your subclasses (and ONLY your subclasses) to access a certain variable there is no reason not to declare it as protected.

提交回复
热议问题