ReadOnly in C # vs in VB.NET

It seems that the VB.NET and C # keyword readonly

have some differences ...

Let's say the ReadOnly property in C # can be assigned under some conditions, but never in VB.NET?

+3


source to share


2 answers


In C #, readonly is a field modifier . It indicates that the field can only be assigned on initialization or in the constructor.



VB.NET is the same except that ReadOnly is also a property modifier . It indicates that the property cannot be assigned, i.e. This is a getter.

+9


source


In VB.NET, a read-only property is usually created to be read only from the outer class. If you want to set this property, you can easily do it from within the class by modifying a valid local variable.

So, for example, in VB 2010

Public ReadOnly Property SomeVariable() As String

      

or in earlier versions,



Private _SomeVariable As String
Public ReadOnly Property SomeVariable() As String
    Get
        Return _SomeVariable
    End Get
End Property

      

you can set it from your class like:

_SomeVariable = somevalue

      

The property value cannot be changed from an external class.

+3


source







All Articles