4

这是允许的:

Public Property Text() As String

而对于只读属性,为什么不允许我使用等价属性?

Public ReadOnly Property Text() As String

我似乎被迫使用:

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

1 回答 1

5

它现在在 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

细节:

于 2016-05-02T14:55:01.750 回答