Foo() 与 this.Foo()

发布于 2024-11-01 05:13:42 字数 219 浏览 0 评论 0原文

我有一位同事使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

小…红帽 2024-11-08 05:13:42

正如这里的其他答案所指出的,这很大程度上是风格问题。

很多时候,如果没有 this.,对 Foo() 的调用将是明确的,但有时它可能会增加清晰度。例如:

EventHandler foo = GetEventHandlerFoo();
EventHandler baz = GetEventHandlerBar();
foo();
this.Bar();
baz();

在本例中,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 the this., but there are times where it might add clarity. For example:

EventHandler foo = GetEventHandlerFoo();
EventHandler baz = GetEventHandlerBar();
foo();
this.Bar();
baz();

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.

韶华倾负 2024-11-08 05:13:42

如果你想用它,就用它。

如果您不想使用它,请不要使用它。

这里没有正确的答案。

如果出于某种原因,您有一个命名约定,其中仅名称不足以确定您正在查看的是局部变量、方法参数、实例字段、属性还是方法,那么您应该修复命名约定,而不是在本地字段前添加 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.

白日梦 2024-11-08 05:13:42

我认为这归结为语法与语义的问题。 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.

抚笙 2024-11-08 05:13:42

这是一个风格的事情。有时人们认为这使代码更加清晰——显然有些人发现明确方法调用的目标很有帮助。就我个人而言,我发现 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.

闻呓 2024-11-08 05:13:42

此示例演示了如何使用 this 关键字显式描述变量的范围:

class Item
{
    int id;

    public Item (int id)
    {
        this.id = id;
    }

}

This example demonstrates how the this keyword can be used to explicitly describe a variable's scope:

class Item
{
    int id;

    public Item (int id)
    {
        this.id = id;
    }

}
橪书 2024-11-08 05:13:42

我总是使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文