Why can't I have auto-implemented Read-only properties

后端 未结 1 518
庸人自扰
庸人自扰 2021-01-19 01:04

This is allowed:

Public Property Text() As String

Whereas for a read-only property why aren\'t I allowed an equivalent?

Pu         


        
1条回答
  •  太阳男子
    2021-01-19 01:11

    It is now supported in VB14 (Visual Studio 2015 and later). Auto-implemented properties can be initialized with initialization expressions:

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

    or in the constructor:

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

    Details:

    • Auto-Implemented Properties (Visual Basic):

    0 讨论(0)
提交回复
热议问题