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

前端之家收集整理的这篇文章主要介绍了.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
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

细节:

> Auto-Implemented Properties (Visual Basic)

猜你在找的VB相关文章