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

发布于 2024-08-28 17:04:08 字数 782 浏览 4 评论 0原文

我的同事一直告诉我评论中列出的事情。

我很困惑。 有人可以帮我揭开这些事情的神秘面纱吗?

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
}

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
}

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

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

发布评论

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

评论(5

秋意浓 2024-09-04 17:04:08

如果没有名称冲突,则 this. 是多余的。仅当需要对当前对象的引用或有与字段同名的参数时才需要它。

线程问题与此无关。混乱可能来自这样一个事实:大多数静态成员都是线程安全的,并且静态成员不能(!)用 this. 调用,因为它们没有绑定到实例。

The 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. since they aren't bound to the instance.

时光是把杀猪刀 2024-09-04 17:04:08

“不使用 this.xxx 会创建线程
问题”

完全是一个神话。只需要求您的同事检查生成的 IL,并让他解释为什么无论您是否添加 this,它们都是相同的。

“在类中使用它,例如
if(this._a == 2)"

取决于您想要实现的目标。您的同事似乎在说总是引用私有字段,这在我看来似乎不明智。通常,您希望访问 public 属性,即使在类内部也是如此,因为 getter 可能会修改值(例如,当列表为 null 时,List 类型的属性可能会返回一个新的 List 实例,以避免访问属性时出现空引用异常)。

"Not using this.xxx creates threading
issues"

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.

"use this when inside the class e.g.
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).

握住你手 2024-09-04 17:04:08

我个人的“最佳实践”是始终使用它。是的,它是多余的,但当您考虑多线程应用程序时,这是从第一眼就识别实例状态被更改或检索的好方法。

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.

不醒的梦 2024-09-04 17:04:08

询问您的同事为什么他认为这些建议是最佳实践可能会有所帮助?人们常常引用他们在某处学到的最佳实践“规则”,但对这些实践背后的原因没有任何真正的了解。

正如 Lucero 所说,“this”不是必需的,除非 () 存在名称冲突。然而,有些人喜欢在不严格要求的情况下包含“this”,因为他们认为它可以增强可读性/更清楚地显示程序员的意图。在我看来,这是个人喜好的问题,而不是其他任何事情。

至于“Bar”方法中的“坏主意”:您的同事可能会认为这种不好的做法,原因如下:如果“A”的 setter 方法被更改为产生一些副作用,则 A=a ; 也会产生这种副作用,而 _a = a; 只会设置私有变量。在我看来,最佳实践是意识到差异而不是偏爱另一种。

最后,“线程问题”是无稽之谈 - AFAIK“这个”与线程无关。

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. 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. 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; will also produce this side effect, whereas _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.

小兔几 2024-09-04 17:04:08

数字 2 是一个神话,通过提及自动属性很容易揭穿它。 自动属性允许您定义属性,而无需由编译器自动生成的支持字段。因此,询问您的同事对自动属性的看法。

The number 2 is a myth that is easily debunked by mentioning automatic properties. 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.

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