简体   繁体   English

始终使用this-keyword作为前缀(自动)属性是一种好习惯吗?

[英]Is always prefixing (auto)properties with the this-keyword considered a good practice?

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. 现在这被auto属性所取代。 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. 现在我发现自己在内部添加了我在内部使用的所有属性this. , to sooth my feeling. ,以舒缓我的感觉。

My dilemma is that before I was always a bit against prefixing all usage of internal members with this. 我的困境是,在我总是有点反对使用内部成员的所有使用前缀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. 似乎意见各不相同,尽管更多的人赞成this.为前缀this. . Before the auto properties I was always pretty much against prefixing with this. 在自动属性之前,我总是反对加上前缀this. instead of in constructors and in setters (as I mentioned before). 而不是在构造函数和setter中(如前所述)。 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. 附加说明:将属性命名为与类相同( public Bar Bar { get; private set; } )也常见的事实也使我倾向于加前缀。 Every time I type Bar.DoMethod() , I feel like it looks like a static method. 每次我输入Bar.DoMethod() ,我觉得它看起来像一个静态方法。 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. 即使VS是彩色Bar如果它是一个静态方法,你也不能拥有一个具有相同签名的静态和实例方法。 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. 当它被着色时,很明显它是一种静态方法,但是当它没有着色时,它不是100%清楚它不是静态方法。 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. 例如,你可能只是缺少一个using语句,但也只是因为我不习惯将未被彩色链接到它是否是静态调用。 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 (Eg the "dot" after foo in (Foo)foo.Bar.DoMethod() ). 在成员的情况下我通过首字母大写或在属性的情况下通过“点”立即看到它之前(例如foo中的“点” (Foo)foo.Bar.DoMethod() ) 。

(Difficult to choose an "Accepted answer" at the moment) (目前很难选择“接受的答案”)

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. 如果您使用某种工具来测试代码以获取编码指南(如ReSharper或Microsoft自己的StyleCop) ,则会在不使用this-reference时向您发出警告,或者如果您没有使用资本启动您的属性。

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. 您在自己的类中调用的属于该类的任何属性,字段或方法都应该使用this-reference作为前缀,以便于阅读。

Update : of course, opinions vary. 更新 :当然,意见各不相同。 I like hitting this. 我喜欢this. and then, after the dot, seeing only the members, instead of seeing all keywords when just hitting ctrl-space without any prefix. 然后,在点之后,只看到成员,而不是在没有任何前缀的情况下点击ctrl-space时看到所有关键字。 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 strongly recommend to use ' this. 我强烈建议使用' 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. 它让您从可读性的角度了解范围,并帮助您避免编译器在编译时报告的愚蠢错误。

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. 我认为唯一可以接受的地方是,它是公开暴露的API的一部分,重命名会导致重大变化。

In the first example, the bar parameter lexically shadows bar field from the instance. 在第一个示例中, bar参数从实例中词汇阴影bar字段。 So you have to use this for disambiguation. 所以你必须用this来消除歧义。

In the 2nd example you have no such ambiguity, and hence does not need a disambiguation (ie this ). 在第二个例子中,你没有这种歧义,因此不需要消歧(即this )。 You can however still prefix it, if that is your cup of tea. 然而,如果那是你的一杯茶,你仍然可以作为前缀。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM