Hawkeye Hawkeye - 1 month ago 6
Vb.net Question

Difference between a one-line ReadOnly property and a ReadOnly with explicit Get statement in vb.net

I have a simple question.

Is there a difference between a one-line

ReadOnly Property
and a
ReadOnly Property
with an explicit
Get
Statement?

For example, In Visual Studio 2010 I used to have to do this:

Private _Message As String = ""

Public ReadOnly Property Message As String
Get
Return _Message
End Get
End Property

Public Sub New(Message As String)
_Message = Message
End Sub


But now here I am in Visual Studio 2015 and it let's me get away with this:

Public ReadOnly Property Message As String

Public Sub New(Message As String)
Me.Message = Message
End Sub


Now I wasn't trying to me lazy, I just ran across it, expecting IntelliSense to force the
Get
Statement. So I'm left wondering if there is a difference that I should be aware of. Is it bad practice or do something different?

I've managed to find tons of topics and forums about
ReadOnly
in C#, but I seem to be coming up dry when searching the topic regarding vb.net.

I'll appreciate any input.
Thanks! =)

Answer

Properties without explicit getters and setters are simply a syntactic convenience. There's no difference in implementation. You should generally use the second option because it makes your code more readable. Only use the first option if you need extra functionality in the getter.