Accessing members in your own class: use (auto)properties or not?

前端 未结 6 1580
温柔的废话
温柔的废话 2021-01-22 21:16

I\'ve created this \"question\" as a community-wiki, because there is no right or wrong answer. I only would like to know how the community feels about this specific issue.

6条回答
  •  生来不讨喜
    2021-01-22 21:56

    I always use instance variables as well. The reason is because properties might be doing stuff like validating arguments (like in a setter) for not null or not empty. If you're using the variable inside your class code, there's no need to go through the extra overhead of those checks (assuming you know the variable value is valid). The properties could be doing other things as well (logging, for example), that are important for the public API, but not for internal usage, so again, it's better to avoid the overhead and just use the instance variable in my opinion.

提交回复
热议问题