Do you use 'this' in front of instance variables?

前端 未结 17 2150
猫巷女王i
猫巷女王i 2020-12-19 01:08

When accessing instance variables or properties of a class from within the class itself, do you prepend them with \"this.\"?

17条回答
  •  天命终不由人
    2020-12-19 02:05

    In C#, I absolutely do. The primary reasons are:

    1. Whether to do it or not is a stylistic issue. In spite of all the fighting that goes on, I don't believe there's an objectively better approach.

    2. My source analysis tool (StyleCop) defaults to requiring this. in front of instance accesses. My first point implies that I shouldn't care much about whether I always do or always don't, and because the default StyleCop setting is to always require it, I take the path of least resistance/greatest consistency and I follow the default setting.

    I follow this philosophy for most stylistic issues. I'm a huge fan of not changing default formatting options in an auto-formatting IDE. It just makes everyone's life harder over something that's really just not that important.

提交回复
热议问题