Do you use 'this' in front of instance var

2019-01-26 07:15发布

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

17条回答
聊天终结者
2楼-- · 2019-01-26 07:44

No, but then I write a lot of VB ;)

About the only time I'll check this/Me is when I don't remember the exact name of the member or when I need to distinguish it with a function parameter of the same name.

查看更多
地球回转人心会变
3楼-- · 2019-01-26 07:47

Anytime I have a method parameter whose name is identical to an instance variable (rarely) I do, to avoid confusion.

查看更多
孤傲高冷的网名
4楼-- · 2019-01-26 07:49

Nope! I can certainly see that it might be beneficial, but nothing I work on is sufficiently complex to need another level of clarification.

查看更多
地球回转人心会变
5楼-- · 2019-01-26 07:50

If your instance variable name is same as method argument- it is no longer "simply a reason to clarify". If you don't do prepend- it can lead to defects.

I think that's what Ben S meant- but just wanted to stress- that its not a matter of best practice any more.

I often do- increases clarity. I don't think it adds to clutter- because our brains quickly reads past it, but registers that it is an instance variable

查看更多
唯我独甜
6楼-- · 2019-01-26 07:57

We're using ReSharper which manages all of that very well. Most of the time we remove 'this' unless keeping it in a constructor since we typically use constructor parameters with the same name.

查看更多
登录 后发表回答