有什么理由使用自动实现的属性而不是手动实现的属性? [英] Any reason to use auto-implemented properties over manual implemented properties?

查看:19
本文介绍了有什么理由使用自动实现的属性而不是手动实现的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解 PROPERTIES 相对于 FIELDS 的优势,但我觉得使用 AUTO 实现的属性而不是 MANUAL 实现的属性并没有真正提供任何优势,除了让代码看起来更简洁一些.

I understand the advantages of PROPERTIES over FIELDS, but I feel as though using AUTO-implemented properties over MANUAL implemented properties doesn't really provide any advantage other than making the code a little more concise to look at it.

我觉得使用起来更舒服:

I feel much more comfortable using:

    private string _postalCode;

    public string PostalCode
    {
        get { return _postalCode; }
        set { _postalCode = value; }
    }

代替:

public string PostalCode { get; set; }

主要是因为如果我想对 get 和 set 进行任何类型的自定义实现,无论如何我都必须创建自己的属性,并由私有字段支持.那么为什么不从一开始就咬紧牙关,立即为所有属性赋予这种灵活性,以保持一致性?考虑到您在 Visual Studio 中所要做的就是单击您的私有字段名称,然后按 Ctrl+E,这真的不需要多花一秒钟的时间,您就完成了.如果我手动执行,那么最终会出现不一致的情况,其中有一些由私有字段支持的手动创建的公共属性,以及一些自动实现的属性.我感觉好多了,因为它始终保持一致,无论是自动还是手动.

primarily because if I ever want to do any kind of custom implementation of get and set, I have to create my own property anyway backed by a private field. So why not just bite the bullet from the start and give all properties this flexibility straight away, for consistency? This really doesn't take but an extra second, considering that all you have to do in Visual Studio is click your private field name, and hit Ctrl+E, and you're done. And if I do it manually, then I end up with inconsistency in which there are SOME manually created public properties backed by private fields, and SOME auto-implemented properties. I feel much better with it being consistent all around, either all auto or all manual.

这只是我吗?我错过了什么吗?我是不是误会了什么?我是否过于强调一致性?我总能找到关于 C# 特性的合法讨论,而且几乎每件事都有利有弊,但在这种情况下,我真的找不到任何人建议不要使用自动实现的属性.

Is this just me? Am I missing something? Am I mistaken about something? Am I placing too much emphasis on consistency? I can always find legitimate discussions about C# features, and there are almost always pros and cons to everything, but in this case, I really couldn't find anyone who recommended against using auto-implemented properties.

推荐答案

除了简洁之外,它并没有给你任何额外的东西.如果您更喜欢更冗长的语法,那么请务必使用它.

It doesn't grant you anything extra beyond being concise. If you prefer the more verbose syntax, then by all means, use that.

使用 auto props 的一个优点是它可以潜在地避免您犯下愚蠢的编码错误,例如不小心将错误的私有变量分配给属性.相信我,我以前做过!

One advantage to using auto props is that it can potentially save you from making a silly coding mistake such as accidentally assigning the wrong private variable to a property. Trust me, I've done it before!

您关于汽车道具不是很灵活的观点是一个很好的观点.您拥有的唯一灵活性是使用 private getprivate set 来限制范围.如果您的 getter 或 setter 对它们有任何复杂性,那么自动道具不再是可行的选择.

Your point about auto props not being very flexible is a good one. The only flexibility you have is by either using private get or private set to limit scope. If your getters or setters have any complexity to them then the auto props are no longer a viable option.

这篇关于有什么理由使用自动实现的属性而不是手动实现的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆