When accessing instance variables or properties of a class from within the class itself, do you prepend them with "this.
"?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
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.
Anytime I have a method parameter whose name is identical to an instance variable (rarely) I do, to avoid confusion.
Nope! I can certainly see that it might be beneficial, but nothing I work on is sufficiently complex to need another level of clarification.
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
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.