Visual Basic .NET Language Read-Only Auto-Properties


Example

Read-only properties were always possible in VB.NET in this format:

Public Class Foo

  Private _MyProperty As String = "Bar"

  Public ReadOnly Property MyProperty As String
    Get
      Return _MyProperty
    End Get
  End Property

End Class

The new version of Visual Basic allows a short hand for the property declaration like so:

Public Class Foo

  Public ReadOnly Property MyProperty As String = "Bar"

End Class

The actual implementation that is generated by the compiler is exactly the same for both examples. The new method to write it is just a short hand. The compiler will still generate a private field with the format: _<PropertyName> to back the read-only property.