When should you use a field rather than a property?

前端 未结 7 1969
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 06:56

Can anyone clearly articulate when you use a field and when to use a property in class design?

Consider:

public string Name;

Or:

7条回答
  •  一整个雨季
    2020-12-06 07:18

    Properties are more maintainable than fields, you can encapsulate logic in your setters/getters, allowing you to hide the implementation.

    They also make refactoring easier.

    More information:

    • Property Usage Guidelines
    • Field Usage Guidelines

提交回复
热议问题