在我在网上阅读的许多文章中说,在 vb.net 中创建属性时,它们应该使用 get/set 方法和类中的私有(private)/ protected 成员变量。
像这样:
Public Class Person
Private _name as string
public property Name as string
get
return _name
end get
set(byval value as string)
_name = value
end set
end property
end class
如果有没有逻辑在属性的获取/设置中,为什么不写这样的相同属性:
Public class Person
Public Property Name as string
end class
这是因为属性只是为了从外部访问类,而您会将变量存储在类中吗?
最佳答案
原因是这些指南和教程是在 VB.NET 4.0 出现之前发布的。没有其他理由不使用自动实现的属性。
关于vb.net 获取/设置属性没有逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928049/