当在一个类中你有一个私有字段并在公共属性上公开该字段时,我应该在类中使用哪个字段?
下面是我试图找出的一个例子。应该操纵 Private Field _Counter 还是 Property Counter?
公开课测试
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
结束类
在此先感谢您的帮助。教育