Is there a way to define the getter function in C # as ReadOnly as VB.NET?

In C #, you can define a readonlygetter function without defining a set function like this:

private int _id;

public int Id
{

   get { return _id; }
   // no setter defined
}

in vb.net

Private _id as Integer
Public Readonly Property Id() As Integer
    Get
       Return _id
    End Get
End Property

Is it possible to mark such a function as readonly, as you can in VB.NET, to be more detailed?

+5
source share
1 answer

I do not know what gives ReadOnlyin VB. I think the most explicit that you can get is actually less verbose:

public int Id { get; private set; }

In C #, ReadOnlyindicates that the field value is set at the time the object is created and does not change after the constructor completes. You can achieve this through:

private readonly int _id; // note field marked as 'readonly'

public int Id
{
   get { return _id; }
}

, (, ) ReadOnly. , , , . , , VB ReadOnly.

, . VB ReadOnly # 1, , :

' Only code inside class employee can change the value of hireDateValue.
Private hireDateValue As Date
' Any code that can access class employee can read property dateHired.
Public ReadOnly Property dateHired() As Date
    Get
        Return hireDateValue
    End Get
End Property

# ReadOnly . , .

, # VB .

+10
source

All Articles