3

Is a readonly field in VB.NET thread safe? For example, see the code below:

Class Customer
  ReadOnly Name As String

  ReadOnly ZIP As Integer = 98112

  Sub New(ByVal Name As String)

    Me.Name = Name

  End Sub
End Class
1
  • By field do you mean a variable or a property?
    – briddums
    Jun 8, 2012 at 14:02

1 Answer 1

4

Yes, if nobody can write it, everyone is synced on its value.

Besides, you could make this integer constant (or shared readonly)

2
  • 1
    you are helpful
    – Anon
    Jun 8, 2012 at 8:25
  • I would strongly recommend making it constant, as this documents that A) ZIP will not change within the constructor and B) that all customers have the same ZIP. B is achievable without A by making it shared.
    – Brian
    Jun 8, 2012 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.