我喜欢.NET自动属性,在C#中声明readonly
属性非常简单,只需将其set
部分声明为private
即可,如下所示:
public String Name{ get; private set; }
但是当我在VB.NET中尝试这样做时,我很惊讶地发现它不被支持,正如这里所提到的,我必须按以下方式编写:
Private _Name as String
Public ReadOnly Property Name as String
Get
return _Name
End Get
End Property
或者:
Private _Name as String
Public Property Name as String
Get
return _Name
End Get
Private Set(value as String)
_Name = value
End Set
End Property
在VB.NET中,这些声明有什么区别?哪一个更受欢迎?为什么?
编辑
哪个声明会影响编译时间、运行时间或性能?