简体   繁体   English

拥有带有私有设置程序或私有字段且拥有属性的自动实现属性的哪个更好?

[英]Which one is better to have auto-implemented property with private setter or private field and property just getter?

My question may be a part of an old topic - "properties vs fields". 我的问题可能是一个旧主题的一部分-“属性与字段”。

I have situation where variable is read-only for outside class but needs to be modified inside a class. 我遇到的情况是,变量对于外部类是只读的,但需要在类内部进行修改。 I can approach it in 2 ways: 我可以通过2种方法来解决:

First: 第一:

private Type m_Field;
public Type MyProperty { get { return m_Field; } }

Second: 第二:

public Type MyProperty { get; private set; }

After reading several articles (that mostly covered benefits of using public properties instead of public fields) I did not get idea if the second method has some advantage over the first one but writing less code. 在阅读了几篇文章之后(大部分内容涵盖了使用公共属性而不是公共领域的好处),我不知道第二种方法是否比第一种有优势,但是编写的代码更少。 I am interested which one will be better practice to use in projects (and why) or it's just a personal choice. 我有兴趣在项目中使用哪种方法更好(为什么),或者这只是个人选择。

Maybe this question does not belong to SO so I apologize in advance. 也许这个问题不属于SO,所以我先向您道歉。

The second version produces less clutter, but is less flexible. 第二个版本产生的杂波较少,但灵活性较差。 I suggest you use the second version until you run into a situation that makes the first version necessary and then refactor - changes will be local to the class anyway, so don't worry too much about that! 我建议您使用第二个版本,直到遇到必须使用第一个版本然后进行重构的情况为止-更改将始终是该类的本地内容,因此不必为此担心太多!

Generally, writing less code is a good idea. 通常,编写更少的代码是一个好主意。 The less code you write, the less bugs you produce :) 您编写的代码越少,产生的错误也越少:)

The second one will pretty much compile down to the first one anyway, so IMO always use the second as it's less & neater code. 无论如何,第二个将几乎可以编译为第一个,因此IMO始终使用第二个,因为它的代码更简洁。

The only scenarios I tend to use the first approach are when I want to lazily load a property eg 我倾向于使用第一种方法的唯一场景是当我想延迟加载属性时,例如

private List<string> _items;
...

public List<string> Items
{
    get
    {
        if (_items == null)
        {
            _items = new List<string>();
            // load items
        }
        return _items;
    }
}

Second version is shorter, so I think it's usually better. 第二个版本较短,所以我认为通常会更好。 The exception is, when the only write access occurs in the constructor. 例外是,当唯一的写访问发生在构造函数中时。 Then I prefer the first version as this allows the field to be marked as readonly . 然后,我更喜欢第一个版本,因为这允许将该字段标记为readonly

For debugging the second is the best. 对于调试,第二个是最好的。 Otherwise you'll have to put breakpoins at each place where you set the field. 否则,您将不得不在设置该字段的每个位置放置断点。 With the second you put one breakpoint on the set of the property. 在第二个步骤中,您在属性集上设置了一个断点。

我个人比较喜欢第二个版本,因为它写的少,所以我可以花些时间做更复杂的编码。。。在我看来,它还可以促进延迟开发。

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

相关问题 具有私有设置器的继承的自动实现属性 - Inherited auto-implemented property with private setter 访问自动实现属性的私有字段 - Accessing the private field of an auto-implemented property 自动实现属性中的私有变量在哪里/在哪里? - Where/what is the private variable in auto-implemented property? 自动实现的属性设置器上的条件断点 - Conditional breakpoint on auto-implemented property setter 引用自动实现属性的支持字段 - Reference to the backing field of auto-implemented property 我可以为 C# 自动实现的属性(也称为自动支持字段)定义自定义 getter 吗? - Can I define a custom getter for a C# auto-implemented property (a.k.a. auto backing field)? 可以仅在自动实现的属性的Setter上设置属性吗? - Can an Attribute be set only on the Setter of an Auto-Implemented Property? 自动实现属性的继承,派生类中仅需要getter - Inheritance of auto-implemented property with only getter needed in a derived class 使用getter和setter声明私有财产有什么好处吗? - Is there any benefit to declaring a private property with a getter and setter? Mono.Cecil自动实现属性访问后备字段 - Mono.Cecil auto-implemented property accessing backing field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM