C#“这个”过度使用?
可能的重复:
你什么时候使用“this”关键字?
我正在写我的第一个 C# 代码,我注意到仅当本地名称 foo
与 this.foo
不同时才需要 this.foo
>。到目前为止,我对于是否使用 this
一直不一致。
是首选始终使用 this
还是仅在必要时才使用 this
?
Possible Duplicate:
When do you use the “this” keyword?
I'm writing some of my first C# code, and I notice that this.foo
is only necessary when there is a local name foo
different from the this.foo
. I have so far been inconsistent as to whether or not I use this
.
Is it preferred to always use this
or is it preferred to only use this
when necessary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我只在必要时使用
this
。大多数时候,我不存在必须区分局部变量和类变量的问题,所以除非有需要,否则我不会打扰I only use
this
when it is necessary. Most of the time I don't have the issue of having to differentiate between a local and class variable, so I just don't bother unless the need comes up我总是输入“this”,因为它允许我使用 Visual Studio 的 Intellisense。我认为对于每个对对象属性或方法的引用使用“this”并没有任何有意义的风格上的反对。
I always type 'this' because it allows me to use Visual Studio's Intellisense. I don't think that there is any meaningful stylistic objection to using 'this' for each reference to an object property or method.
这实际上是一个风格问题 - 有些人喜欢一直使用
this
,因为它可以清楚地表明您何时访问成员变量而不是方法本地,有些人在名称上使用疣目的相同,但有些人两者都不使用。无论如何,对本地方法使用与成员变量相同的名称可能是一个坏主意 - 我认为它合法的唯一一次是在构造函数或其他初始化例程中设置这些成员时。
It's really a matter of style - some people like to use
this
all the time, since it makes it clear when you're accessing a member variable instead of method local, some people use warts on the name for the same purpose, and some people don't use either.Regardless, it's probably a bad idea to use the same name for a method local as for a member variable - the only time I'd consider it legitimate is when setting those members in the constructor or other initialization routine.
我确实更喜欢它,因为它很清晰。而且还因为我们遵循 StyleCop 的规则(几乎所有规则:))。
为什么 StyleCop 建议在方法或属性调用前加上前缀“这个”?
I do prefer it because of clarity. But also because we follow StyleCop's rules (almost all of them :) ).
Why does StyleCop recommend prefixing method or property calls with "this"?
这完全取决于您的偏好。我个人喜欢一直使用它来强调它是一个类变量,但这只是我。
It's based entirely on your preference. I personally like to use it all the time to emphasize that it's a class variable, but that's just me.
当我想澄清我的意图时,我会使用
this
。但一般来说,如果您将鼠标悬停在相关成员上,Visual Studio IDE 会为您澄清该成员(通过弹出窗口)。这减少了对
this
的需求。正如 Adam 指出的,首先输入
this
只会为您提供本地成员的智能感知,而不是为您提供整个智能感知列表。I use
this
when I want to clarify my intent.But generally, the Visual Studio IDE will clarify the member for you (with a popup) if you hover over the member in question. That reduces the need for
this
.As Adam points out, typing
this
first will intellisense only the local members for you, instead of giving you the entire intellisense list.只是为了给出一个与许多“pro this”答案形成对比的答案......
我几乎从不使用
this
。我在所有成员变量前面加上下划线,这样我就可以轻松区分局部变量和成员变量。我发现一直使用this
确实会污染代码并使其变得更加嘈杂。Just to give an answer that contrasts with the many "pro this" answers...
I almost never use
this
. I prefix all member variables with an underscore, and so I can easily tell local and member variables apart. I find usingthis
all the time really pollutes the code and makes it far more noisy.