Best Practice on local use of Private Field x Prop

2019-02-27 14:54发布

When inside a class you have a private fiels and expose that field on a public property, which one should I use from inside the class?

Below you is an example on what I am trying to find out. Should manioulate the Private Field _Counter or the Property Counter?

Public Class Test

Private _Counter As Integer

Public Property Counter() As Integer
    Get
        Return _Counter
    End Get
    Set(ByVal value As Integer)
        _Counter = value
    End Set
End Property

Private Sub Dosomething()

    'What is the best practice?
    'Direct access to private field or property?

    'On SET
    _Counter += 1
    'OR
    Me.Counter += 1

    'On Get
    Console.WriteLine(_Counter)
    Console.WriteLine(Me.Counter)

End Sub

End Class

Thanks in advance for the help. Edu

9条回答
混吃等死
2楼-- · 2019-02-27 15:46

Thank you all for the answers and suggestions.

After considering all the suggestions here plus other researches it is my impression that for this situation on Private Field versus Assessor it is more of a personal choice. So basically the most important is that no matter what you choose be consistent.

That said; my personal rule is leaning towards this:

  1. Access your private fields directly.

  2. If accessing accessors use the keyword ME. to improve readability

  3. Use the accessor only if it implements vital logic logic that also applies to private access. This way you know that if you are using the accessor it is because there is "something else to it"

  4. Avoid using Protected Fields. Derived classes should always use the accessor, never direct access to the field.

Let me know what you think.

SideNote:

After this I think we are missing a new scope for the class level fields. A keyword like “Restricted” where this field could only be accessed from its getter/setter. This way you always access directly the private fields, but if you need to make sure certain field can only be accessed by its accessor that you change the Private to Restricted. (how about "Restricted , RestrictedRead and RestrictedWrite"?)

查看更多
等我变得足够好
3楼-- · 2019-02-27 15:48

Use the private field because you are not doing something in specific in the setter.

I would also recommend to remove the property-setter, this way you force the state of the counter to be set by the given method DoSomething()

查看更多
在下西门庆
4楼-- · 2019-02-27 15:53

IMO you should be using the Property accessor whenever possible. This is because you don't have to worry about any internal logic that might be available when you have an a property.

A good example of where this happens is in the code behind in a Linq DataContext.

check this out...

[Column(Storage="_ReviewType", DbType="TinyInt NOT NULL")]
public byte ReviewType
{
    get
    {
        return this._ReviewType;
    }
    set
    {
        if ((this._ReviewType != value))
        {
            this.OnReviewTypeChanging(value);
            this.SendPropertyChanging();
            this._ReviewType = value;
            this.SendPropertyChanged("ReviewType");
            this.OnReviewTypeChanged();
        }
    }
}

Notice all that logic in the 'setter'?

This is why it's important to start getting into the practice of calling your Properties instead of fields, IMO.

查看更多
登录 后发表回答