Foo() 与 this.Foo()
我有一位同事使用 C# 重构工具。该工具出于某种原因更喜欢:
this.Foo()
现在
Foo()
我们要求他将其关闭,只是因为自动重新编写所有代码很烦人,但这不是重点。
我错过了什么,还是这只是错误的?我到底为什么想要 this.Foo()
?
I have a co-worker who uses a C# refactoring tool. The tool for some reason perfers:
this.Foo()
over
Foo()
Now we've asked him to turn it off simply because it's annoying to have all the code re-written automatically, but that's not the point.
Am I missing something, or is this just wrong? Why on earth would I want this.Foo()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如这里的其他答案所指出的,这很大程度上是风格问题。
很多时候,如果没有
this.
,对Foo()
的调用将是明确的,但有时它可能会增加清晰度。例如:在本例中,
this.
前缀有助于使方法调用从委托调用中脱颖而出。这是必要吗?不,Lasse V. Karlsen 正确地指出
this.
只是命名约定的替代,使其明确。但我发现出于类似的原因,我会经常在本地属性上使用
this.Foo
前缀,这又是一个风格问题。As other answers here point out, this is largely a matter of style.
Many times a call to
Foo()
would be unambiguous without thethis.
, but there are times where it might add clarity. For example:In this case, the
this.
prefix helps to make the method invocation stand out from the delegate invocations.Is this necessary? No, and Lasse V. Karlsen correctly points out that the
this.
is just a surrogate for a naming convention that makes it unambiguous.But I find that I will frequently use the
this.Foo
prefix on local properties for a similar reason, where again it is more a matter of style.如果你想用它,就用它。
如果您不想使用它,请不要使用它。
这里没有正确的答案。
如果出于某种原因,您有一个命名约定,其中仅名称不足以确定您正在查看的是局部变量、方法参数、实例字段、属性还是方法,那么您应该修复命名约定,而不是在本地字段前添加
this.
前缀以将其与本地变量区分开来。If you want to use it, use it.
If you don't want to use it, don't use it.
There's no right answer here.
If for some reason you have a naming convention where the name alone isn't enough to determine whether you're looking at a local variable, a method parameter, an instance field, a property, or a method, you should fix the naming convention, instead of prefixing local fields with
this.
to distinguish them from local variables.我认为这归结为语法与语义的问题。
this.Foo()
的论点是它非常明确地表示 Foo() 是什么以及它来自哪里。毫无疑问,它是传递给方法的参数,还是在谁知道的地方声明的静态类。它是当前对象的方法。如果少了任何东西,你就会放弃清晰的语义,转而采用更严格的语法。反对它的明显论据是:鼠标悬停和 ctrl+单击。
I think this comes down to a question of syntax vs. semantics. The argument for
this.Foo()
is it very explicitly denotes exactly what Foo() is and where it came from. There's no wondering if it was an argument passed in to a method, or a static class declared who-knows-where. It's a method on the current object, period. Anything less and you're giving up clear semantics in favor of tighter syntax.The obvious argument against it: Mouse-over and ctrl+click.
这是一个风格的事情。有时人们认为这使代码更加清晰——显然有些人发现明确方法调用的目标很有帮助。就我个人而言,我发现
this
的虚假使用会分散注意力。It's a style thing. Sometimes people think it makes the code more clear -- apparently some people find it helpful to be explicit about the target of a method invocation. Personally, I find the spurious use of
this
to be distracting.此示例演示了如何使用
this
关键字显式描述变量的范围:This example demonstrates how the
this
keyword can be used to explicitly describe a variable's scope:我总是使用
this.
因为它明确地显示了该东西的位置。我什至不使用重构工具来做到这一点。我用手打字。I always use
this.
since it explicitly shows where the thing is. I don't even use a refactoring tool to do it. I type it by hand.