简体   繁体   English

关于最佳实践的C#神话?

[英]C# myths about best practices?

My colleague keeps telling me of the things listed in comments. 我的同事一直告诉我评论中列出的内容。

I am confused. 我很迷惑。 Can somebody please demystify these things for me? 有人可以为我揭开这些神秘面纱吗?

class Bar
{
    private int _a;

    public int A
    {
        get { return _a; }
        set { _a = value; }
    }

    private Foo _objfoo;

    public Foo OFoo
    {
        get { return _objfoo; }
        set { _objfoo = value; }
    }

    public Bar(int a, Foo foo)
    {
        // this is a bad idea
        A = a;
        OFoo = foo;
    }

    // MYTHS
    private void Method()
    {
        this.A    //1 -
        this._a   //2 - use this when inside the class e.g. if(this._a == 2)
        A         //3 - use this outside the class e.g. barObj.A
        _a        //4 - 
                  // Not using this.xxx creates threading issues.
    }
}
class Foo
{
    // implementation
}

The this. this. is redundant if there isn't a name collision. 如果没有名称冲突,则是多余的。 You only need it when you need a reference to the current object or if you have an argument with the same name as a field. 只有当您需要对当前对象的引用或者您有一个与字段同名的参数时,才需要它。

Threading issues have nothing to do with it. 线程问题与它无关。 The confusion maybe comes from the fact that most static members are implemented so that they are thread-safe and static members cannot (!) be called with this. 混淆可能来自这样的事实:大多数静态成员都是实现的,因此它们是线程安全的,静态成员不能用this.来调用(!) this. since they aren't bound to the instance. 因为他们没有绑定到实例。

"Not using this.xxx creates threading issues" “不使用this.xxx会产生线程问题”

is a complete myth. 是一个完整的神话。 Just ask your co-worker to check the generate IL and have him explain why they are the same whether you add this or not. 只要问问你的同事,检查产生IL,让他解释为什么他们不管添加相同this与否。

"use this when inside the class eg if(this._a == 2)" “在课堂内使用这个,例如if(this._a == 2)”

is down to what you want to achieve. 归结为你想要达到的目标。 What your co-worker seems to be saying is always reference the private field, which does not seem to me sensible. 你的同事似乎在说什么总是参考私人领域,这在我看来并不合理。 Often you want to access the public property, even inside a class, since the getter may modify the value (for instance, a property of type List may return a new List instance when the list is null to avoid null reference exceptions when accessing the property). 通常你想访问公共属性,甚至是在类中,因为getter可能会修改值(例如,当列表为null时,List类型的属性可能会返回一个新的List实例,以避免在访问属性时出现空引用异常)。

My personal "best practice" is to always use this. 我个人的“最佳实践”是始终使用它。 Yes it's redundant but it's great way to identify from the first look where the state of the instance is chaged or retrieved when you consider multi-threaded app. 是的,它是多余的,但是当您考虑多线程应用程序时,从第一个外观中识别实例的状态被查找或检索的很好的方法。

It may help to ask your co-worker why he considers these suggestions are best practice? 询问您的同事为什么认为这些建议是最佳做法可能会有所帮助? Often people quote best-practice "rules" that they have picked up somewhere without any real understanding of the reasons behind the practices. 通常人们会引用最佳实践“规则”,这些规则是他们在没有真正了解实践背后的原因的情况下获得的。

As Lucero says, the "this" is not required unless () there is a name collision. 正如Lucero所说,除非()存在名称冲突,否则不需要“this”。 However, some people like to include the "this" when it is not strictly required, because they believe it enhances readability / more clearly shows the programmers intentions. 然而,有些人喜欢在没有严格要求的情况下加入“this”,因为他们认为它提高了可读性/更清楚地显示了程序员的意图。 In my opinion, this is a matter of personal preference rather than anything else. 在我看来,这是个人偏好而不是其他任何事情。

As for the "bad idea" in your "Bar" method: Your co-worker may consider this bad practice for the following reason: if the setter method for "A" is altered to have some side effect then A=a; 至于你的“Bar”方法中的“坏主意”:你的同事可能会因为以下原因而考虑这种不良做法:如果“A”的setter方法被改变为有一些副作用,那么A = a; will also produce this side effect, whereas _a = a; 也会产生这种副作用,而_a = a; will just set the private variable. 将只设置私有变量。 In my view, best practice is a matter of being aware of the difference rather than prefering one over another. 在我看来,最佳实践是了解差异而不是偏爱另一个。

Finally, the "threading issues" are nonsense - AFAIK "this" has nothing to do with threading. 最后,“线程问题”是无意义的 - AFAIK“this”与线程无关。

The number 2 is a myth that is easily debunked by mentioning automatic properties. 数字2是一个神话,通过提及自动属性很容易被揭穿。 Automatic properties allow you to define a property without the backing field which is automatically generated by the compiler. 自动属性允许您定义没有由编译器自动生成的后备字段的属性。 So ask your co-worker what is his opinion about automatic properties. 所以问你的同事他对自动属性的看法是什么。

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

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