总是在 (auto) 属性前加上 this-keyword 是否被认为是一个好的做法?

发布于 2024-08-18 05:23:36 字数 1650 浏览 4 评论 0原文

自从我发现汽车属性以来,我就尝试在任何地方使用它们。以前,我在类中使用的每个属性总是有一个私有成员。现在它已被 auto 属性取代。我以通常使用普通成员字段的方式在类中使用该属性。问题是该财产以国会大厦开始,恕我直言,以这种方式使用它时看起来有点奇怪。我之前不介意属性以国会大厦开头,因为它们总是在“点”后面。现在,我发现自己在内部使用的所有属性前都加上了 this. 前缀,以安抚我的感觉。

我的困境是,之前我总是有点反对在所有内部成员的使用前加上 this. 前缀,除非“必要”(比如在 setter 或构造函数中)。所以我想就此寻求第二意见。有没有标准的好方法来做到这一点?我是否应该停止抱怨(我有成为“ant humper”(荷兰语表达)的倾向)?

之前:

class Foo
{
    private Bar bar;
    public Bar Bar { get { return bar; } }

    public Foo(Bar bar)
    {
        this.bar = bar;
    }

    public void DoStuff()
    {
        if(bar != null)
        {
            bar.DoMethod();
        }
    }
}

之后:

class Foo
{
    public Bar Bar {get; private set;}

    public Foo(Bar bar)
    {
        this.Bar = bar;
        // or
        Bar = bar;
    }

    public void DoStuff()
    {
        if(this.Bar != null)
        {
            this.Bar.DoMethod();
        }
        // or
        if(Bar != null)
        {
            Bar.DoMethod();
        }
    }
}

更新

似乎意见各不相同,尽管更多人赞成使用 this. 前缀。在使用自动属性之前,我总是非常反对在构造函数和 setter 中使用 this. 前缀(正如我之前提到的)。但现在我不知道了。

附加说明:事实上,将属性命名为与类相同的名称(public Bar Bar { get; private set; })也很常见,这也使我倾向于添加前缀。每次我输入 Bar.DoMethod() 时,我都觉得它看起来像一个静态方法。即使 VS 会为 Bar 着色,如果它是静态方法,并且您不能拥有具有相同签名的静态方法和实例方法。当它被着色时,很明显它是一个静态方法,但是当它没有着色时,并不能 100% 清楚它不是一个静态方法。例如,您可能只是缺少 using 语句,但也只是因为我不习惯必须将未着色的链接与它是否是静态调用相关联。在我立即通过第一个字母的大写(如果是成员)或“点”(如果是属性)立即看到它之前(例如 ( 中 foo 后面的“点”) Foo)foo.Bar.DoMethod())。

(目前很难选择“接受的答案”)

Ever since I found out about auto properties, I try to use them everywhere. Before there would always be a private member for every property I had that I would use inside the class. Now this is replaced by the auto property. I use the property inside my class in ways I normally would use a normal member field. The problem is that the property starts with a capitol, which makes it look a bit weird imho when using it in this manner. I didn't mind that properties start with a capitol before because they would always be behind a "dot". Now I have found myself prefixing all the properties I use internally with this., to sooth my feeling.

My dilemma is that before I was always a bit against prefixing all usage of internal members with this., unless "necessary" (like in a setter or constructor). So I am kind of looking for a second opinion on this. Is there a standard good way to do this? Should I just stop complaining (I have the tendency to be a "ant humper" (Dutch expression))?

Before:

class Foo
{
    private Bar bar;
    public Bar Bar { get { return bar; } }

    public Foo(Bar bar)
    {
        this.bar = bar;
    }

    public void DoStuff()
    {
        if(bar != null)
        {
            bar.DoMethod();
        }
    }
}

After:

class Foo
{
    public Bar Bar {get; private set;}

    public Foo(Bar bar)
    {
        this.Bar = bar;
        // or
        Bar = bar;
    }

    public void DoStuff()
    {
        if(this.Bar != null)
        {
            this.Bar.DoMethod();
        }
        // or
        if(Bar != null)
        {
            Bar.DoMethod();
        }
    }
}

Update

It seems that opinions vary, although more people are in favor of prefixing with this.. Before the auto properties I was always pretty much against prefixing with this. instead of in constructors and in setters (as I mentioned before). But now I just don't know anymore.

Additional note: The fact that it is also common to name the property the same as the class (public Bar Bar { get; private set; }) also makes me tend towards prefixing. Every time I type Bar.DoMethod(), I feel like it looks like a static method. Even though VS would color Bar if it was a static method and you cannot have a static and instance method with the same signature. When it is colored it is clear that it is a static method, but when it is not colored it is not 100% clear that it is not a static method. You could for example just be missing a using statement, but also just because I am not used to having to link the not being colored to whether it's a static call or not. Before I'd instantly see it by the capitalization of the first letter in case of a member or by the "dot" in case of a property (E.g. the "dot" after foo in (Foo)foo.Bar.DoMethod()).

(Difficult to choose an "Accepted answer" at the moment)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

旧伤慢歌 2024-08-25 05:23:36

是的,有一个“执行此操作的标准方法”:大写字母和 this 前缀被认为是良好的编码实践。如果您使用某种工具来测试代码是否符合编码指南,例如 ReSharper 或 Microsoft 自己的 StyleCop,如果不使用 this-reference,或者不以大写字母开头,它会警告您。

您的财产是公开可见的。任何公共财产、领域或方法都应该以大写字母开头。

您在自己的类中调用的任何属性、字段或方法(属于该类的一部分)都应以 this 引用作为前缀,以便于阅读。

更新:当然,意见不同。我喜欢点击 this.,然后在点之后只看到成员,而不是在不带任何前缀的情况下按 ctrl-space 时看到所有 关键字。这对我有帮助。但是,最后(引用从这里开始):

无论您的意见如何,重要的
事情是所有人都密切关注
在项目上进行协作使用
相同的格式标准,
不管这些标准是什么
是。

更多参考:
Microsoft 在几乎所有名称和具体属性中使用大写字母
更多指南

Yes, there is a "standard way to do this": the capital letter and the this-prefix are considered good coding practice. If you use some tool to test your code for coding guidelines like ReSharper or Microsoft's own StyleCop, it will warn you if not using the this-reference, or if you don't start your properties with a capital.

Your properties are publicly visible. Any public property, field or method should start with a capital.

Any property, field or method that you call inside your own class that is part of that class should be prefixed with the this-reference for ease-of-reading.

Update: of course, opinions vary. I like hitting this. and then, after the dot, seeing only the members, instead of seeing all keywords when just hitting ctrl-space without any prefix. This helps me. But, in the end (quote from here):

Whatever your opinion, the important
thing is that all people closely
collaborating on a project use the
same formatting standards,
irrespective of what those standards
are.

More references:
Microsoft on using a capital letter in almost any name and in properties specifically.
More guidelines here.

趴在窗边数星星i 2024-08-25 05:23:36

我强烈建议尽可能使用“this.”。 框架设计指南推荐这种做法。它可以让您从可读性的角度了解范围,并帮助您避免编译器在编译时可能报告的愚蠢错误。

I strongly recommend to use 'this.' where possible. Framework Design Guidelines recommends this practice. It lets you know the scope from readability point of view and helps you avoid silly mistakes which compiler may report at compile time.

轮廓§ 2024-08-25 05:23:36

我强烈建议永远不要使用它,因为它只会降低清晰度。如果您确实发现自己处于需要此以避免冲突的情况,我建议重命名其中一个字段/属性/变量。

我认为它唯一可以接受的地方是,如果它是公开暴露的 API 的一部分,那么重命名会导致重大更改。

And I strongly recommend never using this as it only ever reduces clarity. If you actually find yourself in an instance where you need this to avoid collisions I would recommend renaming one of the fields/properties/variables.

The only place I find it acceptable is if it's part of a publicly exposed API where renaming would cause a breaking change.

鯉魚旗 2024-08-25 05:23:36

在第一个示例中,bar 参数在词法上隐藏了实例中的 bar 字段。所以你必须使用 this 来消除歧义。

在第二个示例中,您没有这样的歧义,因此不需要消歧(即 this)。不过,如果您喜欢的话,您仍然可以在它前面添加前缀。 :)

In the first example, the bar parameter lexically shadows bar field from the instance. So you have to use this for disambiguation.

In the 2nd example you have no such ambiguity, and hence does not need a disambiguation (ie this). You can however still prefix it, if that is your cup of tea. :)

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