为什么我不能自动实现只读属性

时间:2013-01-11 08:09:09

标签: .net vb.net

这是允许的:

Public Property Text() As String

对于只读属性,为什么我不允许等效?

Public ReadOnly Property Text() As String

我似乎被迫使用:

Public ReadOnly Property Text() As String
    Get
        Return fText
    End Get
End Property

1 个答案:

答案 0 :(得分:4)

VB14现在支持它(Visual Studio 2015及更高版本)。可以使用初始化表达式初始化自动实现的属性:

Public ReadOnly Property Text1 As String = "SomeText"
Public ReadOnly Property Text2 As String = InitializeMyText()

或在构造函数中:

Public ReadOnly Property Text As String

Public Sub New(text As String)
    Me.Text = text
End Sub

详细说明: